0

my problem with the following javascript function:

function ValidateDates() {

var valid = false;
birthD = $("#cp1_txtBirthDate").val();
initialD = $("#cp1_txtInitialDate").val();
var regexp = new RegExp("^([1-9]|(0|1|2)[0-9]|30)(/)([1-9]|1[0-2]|0[1-9])(/)((20|19|18)[0-9]{2})$");

if (birthD != "__/__/____" && initialD != "__/__/____") {
    if (regexp.test(initialD) && regexp.test(birthD)) {

        $.get("ValidateDates.aspx?BirthD=" + birthD + "&InitialD=" + initialD, function (data) {
            if (data == 0) {
                valid = true;

                $("#Dates_span").html("");
            }

            else {
                $("#Dates_span").html("*" + data);
                valid = false;

            }

        });

    }
}

return valid;
}

here when i check the variable valid i found it "false" even if its true, because the initial for it is false from the beginning of function so how to solve it and what is wrong?

1
  • You have not described clearly what is wrong. Commented Mar 26, 2012 at 8:56

2 Answers 2

6

When you're doing an asynchronous call, you can't return a value like that. Instead, you should pass in a callback:

function ValidateDates(callback) {

    var valid = false;
    birthD = $("#cp1_txtBirthDate").val();
    initialD = $("#cp1_txtInitialDate").val();
    var regexp = new RegExp("^([1-9]|(0|1|2)[0-9]|30)(/)([1-9]|1[0-2]|0[1-9])(/)((20|19|18)[0-9]{2})$");

    if (birthD != "__/__/____" && initialD != "__/__/____") {
        if (regexp.test(initialD) && regexp.test(birthD)) {

            $.get("ValidateDates.aspx?BirthD=" + birthD + "&InitialD=" + initialD, function(data) {
                if (data == 0) {
                    valid = true;

                    $("#Dates_span").html("");
                }

                else {
                    $("#Dates_span").html("*" + data);
                    valid = false;

                }
                callback(valid);
            });
        }
    }
}

Then, call it like:

ValidateDates(function(isValid)
{
   // Do something with isValid
});
Sign up to request clarification or add additional context in comments.

4 Comments

sorry but i am stuck with something, i want the returned value to use it in if condition with other function like this :if (Page_ClientValidate() && ValidateName() && ValidNull && ValidateDates) so how to deal with this?
If ValidateDates is the only async one, you can just put the others inside the function. Otherwise, you can chain the async calls using multiple functions.
how to make chain because i have many ajax calls for validation and i am using validatePage function that contains all validation to check before submit the page as "client side function" and "has returned value true or false
@speciallife, you could make a server-side wrapper function to do all the validation in one AJAX call. Alternatively, see this answer.
1

What's wrong is that $.get is an asynchronous call, what that means is that the function doesn't wait until the result is returned from $.get call. It just makes the call and continues execution - so valid = true is set long after false has been returned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.