1

I am (was) validating and submiting my form using:

$(documend).ready(function(){
//setting variables

ext_form.submit(function(){
   if(validateExt_email() & validateExt_pass()){

   var ext_email = $('#ext_email').attr('value');
   var ext_pass = $('#ext_pass').attr('value');

   $.ajax({
      type: "post",
      url: "validation_ext.php",
      data: "ext_email="+ ext_email +"& ext_pass="+ ext_pass,
      success: function (){
         $('#content').load('p_order_ok.php');
      }
   })
      return false;
   }else
    return false;
});

//validation rules
});

I think it stopped working when I changed my email validation rule to:

//validation rules
//e-mail
function validateExt_email(){
var ext_a = $("#ext_email").val();

var emailXHR =   $.ajax({
                   type: "post",
                   url: "check_email_ext.php",
                   data: "email="+ext_a,
                   success: function(rsp){
                     //if it exists in our db
                     if(rsp == "exists"){
                       //do something
                       return true;
                     }
                     else
                     //if it's valid but not in our db
                     if(rsp == "ok" ){
                       // do something else
                       return false;
                     }
                     else
                     //if it's NOT valid
                     if(rsp == "invalid"){
                       //do something different
                       return false;
                     }   
                   }
                 }).success = function() {
}
//continuing validation
)};

Now is everything is validated and I hit my submit button, instead of my form posting in "validation_ext.php" is posting (as firebug shows) in "check_email_ext.php".

Any help/suggestions?

Thank you

Edit.... I gave up on this... I just used a filter and regex to validate the email. (the easy way)

Thank you all

3 Answers 3

2

I think you're going wrong where the validateExt_email() function does not return a boolean (but rather, undefined) because $.ajax fires off a request asynchronously and JavaScript doesn't wait for the success callback to fire. That means in this condition:

if(validateExt_email() & validateExt_pass()) { // always false

because validateExt_email() always evaluates to false (undefined is falsy).

You can refactor your solution such that the entire form submit only happens once the ajax validation functions have returned, i.e. success is the key, e.g.:

ext_form.submit(function(){

    // your email ajax checker
    var emailXHR = $.ajax({
                       type: "post",
                       url: "check_email_ext.php",
                       ...
                   }).success = function() {
                       // do next validation step
                   });
2
  • How do I correct this? Where exactly should I define it? (in my three success if... I am telling it when it's true or false)
    – Pavlos1316
    Aug 8, 2011 at 4:49
  • My last line is a bit different .success = function() {} // do next validation }); Is it ok??? or am I missing something?
    – Pavlos1316
    Aug 8, 2011 at 5:11
0

That's what your code says to do.

It says, whenever the form is submitted:

  1. Call validateExt_email which makes a request to check_email_ext.php
  2. Call validateExt_pass
  3. If both functions return true, post to validation_ext.php
  4. Return false

validateExt_email never returns any value, so your condition never passes.

3
  • ah... yes... it is called when i enter an email, or when I press submit... but even the email is correct, it never submits to validation_ext.php. And it was submiting when I wasn't posting to an external file for my email valitation.
    – Pavlos1316
    Aug 8, 2011 at 4:42
  • Your validateExt_email function never returns any value. It just dispatches an AJAX request then ends. The AJAX request's success callback function will be called long after validateExt_email has already returned. Returning true/false there does not do what you think. You need to rethink your logic. Aug 8, 2011 at 4:44
  • I have not said anything about your e-mail validation code (which I can't see), only your logic in processing the form submission. Aug 8, 2011 at 4:56
-1

you need to change to async: false in your options.

Also your scope might not work. You see the function(e) will return true or false. That's an anonymous function. What you should do is set a value that has the same scope as your submit handler and then on the last line, return that.

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.