0

I am trying to create a special ZIP code validation based on database values. Therefore I am checking the values by ajax:

jQuery.validator.addMethod("validate_country_from", function(value, element) 
{
   $.ajax({
      type: "POST",
      url: "ajax.php",
      data: "action=validate_countryzip&direction=1&zip=" + 
         escape(document.getElementById("zip_from").value) + "&country=" + 
         escape(document.getElementById("country_from").value),
      async: false
   }).done(function(msg)
   {
      if(msg == "true")
      {
         return true;
      }
      else
      {
         return false;
      }
   });
}, addressError);

And I am assigning the function in the rules by these rules:

zip_from: {
   required: true,
   validate_country_from: true
},
country_from: {
   required: true,
   validate_country_from: true
},

The ajax request is working fine, and it is done synchronous, the returned value is also correct but still my validation is telling me that there is an error on the two fields.

I hope someone can help...

0

2 Answers 2

1

I think you're mixing your jQuery AJAX methods a little bit there. I've seen done() used after get() before, but never after ajax(). Try

jQuery.validator.addMethod("validate_country_from", function(value, element) 
{
   $.ajax({
      type: "POST",
      url: "ajax.php",
      data: "action=validate_countryzip&direction=1&zip=" + 
         escape(document.getElementById("zip_from").value) + "&country=" + 
         escape(document.getElementById("country_from").value),
      async: false,
      success: function(msg){
          if(msg == "true")
          {
              return true;
          }
          else
          {
              return false;
          }
      },
      error: function(x, s, e){
          return false;
      }
   });
}, addressError);
Sign up to request clarification or add additional context in comments.

Comments

0

Thank you for the answer, but I found the reason: my "done" function is returning a value to the ajax request action, not to the validation method (anonymous delegates are great but sometimes really confusing).

A correct version would be something like this:

jQuery.validator.addMethod("validate_country_from", function(value, element) 
{
   var test = $.ajax({
      type: "POST",
      url: "ajax.php",
      data: "action=validate_countryzip&direction=1&zip=" + 
         escape(document.getElementById("zip_from").value) + "&country=" + 
         escape(document.getElementById("country_from").value),
      async: false
   }).done(function(msg)
   {
   });

   if(test.responseText == "true")
   {
      return true;
   }
   else
   {
      return false;
   }
}, addressError);

However, this is not the final solution because it is not catching any errors and so on.

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.