我正在创建用户注册表单。我有一个电子邮件文本框,我正在尝试测试用户的电子邮件是否已存在于数据库中。
这是我的验证功能:
this.isEmailNotExist = function(mail,callback){
var result = false;
var mailEn = enCryptURL(mail);
var isExist = getURL() + WebConfig.RACustomer + "?$filter=WebAccount eq '" + mailEn + "'";
ajaxRequestCallback(isExist,"GET","json","application/json",function(customer){
if(customer.length <= 0) {
result = true;
}else{
result = false;
}
callback(result);
});
};
然后我将它添加到jquery验证器方法:
$.validator.addMethod(
"isEmailExist",
function(value) {
var cl = new CanvasLoader('register-canvasloader-container');
cl.setDiameter(17);
cl.setDensity(13);
cl.setRange(1.7);
cl.setSpeed(1);
cl.setFPS(20);
cl.show();
_customer.isEmailNotExist(value,function(result){
cl.hide() ;
return result;
});
}
);
$("#registerForm").validate({
onkeyup: false,
rules: {
email: {
required: true,
email: true,
maxlength: 30,
isEmailExist : true
}
},
messages: {
email: {
email : "Please enter a valid email address",
isEmailExist : "This email is already existed"
}
}
});
});
问题:在文本框中输入电子邮件后,显示This email is already existed
而未执行我刚刚在jquery验证方法中添加的isEmailExist
函数。
如何在ajax调用中添加async : false
来解决此问题。
已更新:得到几个答案后,我在这里做了:
$("#registerForm").validate({
rules: {
email: {
required: true,
email: true,
maxlength: 30,
remote: {
url:getURL() + "?$filter=email eq '" + $('#email') + "'",
type:'GET',
dataType:'json',
contentType: 'application/json',
success: function(response){
if(response.length > 0){
console.log("user is existed");
return false;
}else{
console.log("new user");
return true;
}
}
}
}
我有一个错误Cannot read property 'length' of null
。