Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6623015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:28:56+00:00 2026-05-25T21:28:56+00:00

I am trying to make some form validation functions. Here is what I have:

  • 0

I am trying to make some form validation functions. Here is what I have:

<script>
$(document).ready(function() {

var myObj = {}; 

$('#username').keyup(function () {

    id = $(this).attr('id');
    validateUsername(id);       

});

function validateUsername(id){

var username = $("#"+id).val();

    $.ajax({
        url : "validate.php",
        dataType: 'json',
        data: 'action=usr_id&id=' + username,
        type: "POST",
        success: function(data) {

            if (data.ok == true) {

                $(myObj).data("username","ok");

            } else {


                $(myObj).data("username","no");

            }

        }
    }); 


} // end validateusername function


$('#submit').click(function(){

    if (myObj.username == "ok")  {

        alert("Username OK");

    } else {

        alert("Username BAD");

    }   


});




}); // end doc ready

So you can see, when a key is pressed in the textbox, it checks if it’s valid. The “data.ok” comes back correctly. The problem is based on the response, I define $(myObj).username. For some reason, I can’t get this value to work outside the validateusername function. When clicking the submit button, it has no idea what the value of $(myObj).username is.

I need to use something like this, because with multiple form fields on the page to validate, I can do something like:

if (myObj.username && myObj.password && myObj.email == "ok")

… to check all my form fields before submitting the form.

I know I must just be missing something basic…. any thoughts?

EDIT: SOLVED

All I had to do was change var myObj = {}; to myObj = {}; and it’s working like a charm. I think I’ve been staring at this screen waaaaay too long!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:28:57+00:00Added an answer on May 25, 2026 at 9:28 pm

    You’re not accessing the data that you stored properly. Access the username value this way:

    $(myObj).data("username")
    

    Resources:

    Take a look at jQuery’s .data() docs.

    Very simple jsFiddle that shows how to properly set and retrieve data with jQuery’s .data() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a form with some dynamic behavior. Specifically, I have
I am trying to pass some JavaScript variables that I have assigned to form
I have a DataGridView in my VB.net Form. I need to make some rows
I have been trying to make sense of some claims that Appcelerator Titanium makes.
I have a javascript function that posts data to a validation script and grabs
I am trying to make a test of a function that does some pretty
I am trying to make my website resize on handheld divices but form some
I have an otherwise functioning rails project where I'm trying to update some form
I'm trying to make some types in Django that map to standard Django types.
I'm trying to make some simple XML with Java and org.w3c.dom, but I got

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.