0

I have a form for user to register new account. I use jquery + ajax to check availability of email address on blur. In Jquery code I used e.preventDefault(); to prevent form submission if there is any error occurs. I tried the existed email address in the email input and click submit the form. It allows form to submit. It should not do this because ajax reponseText return false. Please anyone tell me what should I fix this code to work? I also read similiar article, i tried but still does not work. Please help me to point out what i need to fix my current code?

Here is my form:

<form role="form" method="post" id="signupForm" action="index.php?view=signup-gv">
                <div class="col-xs-6 border-right">
                    <div class="form-group">
                        <label for="exampleInputEmail1">Full Name</label>
                        <input type="text" class="form-control" id="regname" name="regname" placeholder="Full Name">
                    </div>
                    <div class="form-group">
                        <label for="exampleInputEmail1">Email Address</label><span id="emailcheck"></span>
                        <input type="email" class="form-control" id="regemail" name="regemail" placeholder="Enter email">
                    </div>
                </div>
                <div class="form-group col-xs-6">
                    <label for="exampleInputPassword1">Password</label>
                    <input type="password" class="form-control" id="regpass" name="regpass" placeholder="Password">
                </div>
                <button style="position:relative; left: 15px; top: 10px;" class="btn btn-default" name="register" id="register">Register</button>
            </form>

Here my jquery code:

$(document).ready(function(){
$('#regname').focus(); 
$('#regemail').blur(emailCheck);
$('#signupForm').submit(function(e) {
var regname = $('#regname'); 
var regemail = $('#regemail'); 
var regpass = $('#regpass'); 
var register_result = $('#register_result'); 
register_result.html('Loading..'); 
if(regname.val() == ''){ 
    regname.focus();
    register_result.html('<span class="errorss"> * Full name can not be blank</span>');
    e.preventDefault();
}
else if ($.trim(regemail.val()).length == 0) {
    regemail.focus(); 
    register_result.html('<span class="errorss">* Email address can not be blank</span>');
    e.preventDefault();
}
 else if (emailCheck()==false) {
    regemail.focus(); 
    register_result.html('<span class="errorss"> This email address is already existed. Please choose another one </span>');
    e.preventDefault();
}
else if(regpass.val() == ''){ 
    regpass.focus();
    register_result.html('<span class="errorss">* Password can not be blank</span>');
    e.preventDefault();
}

});
});
function emailCheck() {
var regemail = $('#regemail'); 
var emailcheck = $('#emailcheck');
emailcheck.html(''); 
var UrlToPass = {regemail:regemail.val()} ;
    $.ajax({ 
    type : 'POST',
    cache: false,
    data : UrlToPass,
    url  : 'emailcheck.php',
    success: function(responseText){ 
        if(responseText == 0){
            return true;
        }
        else{
            emailcheck.html('<span class="errorss"> This email is existed.</span>');
            return false;
        }
    }
    });
}
Vy Le
  • 69
  • 1
  • 5

1 Answers1

1

So, the problem is: you're trying to return from an asynchronous function. Your function emailCheck have already ended without returning anything when you try to return "true" or "false" inside your success callback function.

So when you declare a function submit, you should be aware of you can only go further with your submit after returning the ajax call. I would suggest you to always return false from your submit, and then, only inside your callback you decide if you submit your code or not. So here, you could something like this:

$('#signupForm').submit(function(e) {
    var regname = $('#regname'); 
    var regemail = $('#regemail'); 
    var regpass = $('#regpass'); 
    var register_result = $('#register_result'); 
    register_result.html('Loading..'); 
    if(regname.val() == ''){ 
        regname.focus();
        register_result.html('<span class="errorss"> * Full name can not be blank</span>');
        return false;
    } else if ($.trim(regemail.val()).length == 0) {
        regemail.focus(); 
        register_result.html('<span class="errorss">* Email address can not be blank</span>');
        return false;
    } else if(regpass.val() == ''){ 
        regpass.focus();
        register_result.html('<span class="errorss">* Password can not be blank</span>');
        return false;
    }

    emailCheck().done(function(r){
        if(r){
            $('#regemail').focus(); 
            $('#register_result').html('<span class="errorss"> This email address is already existed. Please choose another one </span>');
            //Reseting to submit bind to not call function again
            $('#signupForm').submit(function(){});
            $('#signupForm').submit();
        }
    }).fail(function(r){
        // Do something if fail.
    });

    return false

});

This is only a workaround for your problem. For more elegant solutions, you should check again the link you mentioned: How to return the response from an asynchronous call?. Especially, in the answer, the section Improving Bad Code

Another fix you should make, you should select your 'span' again using the same selector in your callback function. So, do this:

$('#emailcheck').html('<span class="errorss"> This email is existed.</span>');

instead of this:

emailcheck.html('<span class="errorss"> This email is existed.</span>');
jmartins
  • 991
  • 4
  • 16
  • Changed as recommended. But it still allows form submission. Means that this part of code `else if (emailCheck()==false) { regemail.focus(); register_result.html(' This email address is already existed. Please choose another one '); e.preventDefault(); }` is ignored. – Vy Le Feb 25 '15 at 17:49
  • Ok. Besides the fix I recommended, there is a problem using the `return` inside of the callback. It will never return in that way you want for the same reason, it is an **asynchronous** call. I will edit my answer, and suggest you another fix. – jmartins Feb 25 '15 at 17:54