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?

Mark Rhodes
  • 10,049
  • 4
  • 48
  • 51
special life
  • 225
  • 1
  • 7
  • 20

2 Answers2

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
});
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • 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? – special life Mar 26 '12 at 09:51
  • 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. – Matthew Flaschen Mar 26 '12 at 13:20
  • 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 – special life Mar 27 '12 at 05:23
  • @speciallife, you could make a server-side wrapper function to do all the validation in one AJAX call. Alternatively, see [this answer](http://stackoverflow.com/questions/995529/how-to-chain-ajax-requests). – Matthew Flaschen Mar 27 '12 at 13:05
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.

scibuff
  • 13,377
  • 2
  • 27
  • 30