Jquery ajax电子邮件可用性检查程序不起作用

时间:2015-02-25 15:43:29

标签: javascript jquery ajax

我有一张表格供用户注册新帐户。我试图实现jquery + ajax来检查电子邮件的可用性。我使用了e.preventDefault();如果用户输入有任何错误,请阻止表单提交。即使ajax响应返回false(表示电子邮件已经存在于数据库中)。但它仍然允许表单提交。它应该显示此错误:"此电子邮件地址已存在。请选择另一个"。请有人告诉我我的jquery代码有什么问题吗?非常感谢你......!

这是我的表格:

<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>

这是我的jquery + Ajax代码:

    $(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't be blank</span>');
        e.preventDefault();
    }
    else if ($.trim(regemail.val()).length == 0) {
        regemail.focus(); 
        register_result.html('<span class="errorss">* Email address can't 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't 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;
            }
        }
        });
}

0 个答案:

没有答案