JQuery 自定义验证,即使为 true 也会显示错误消息
我创建了一个自定义验证规则(uniqueUserName)。我遇到的问题是,即使从自定义验证方法返回 true,自定义规则也始终会显示错误消息。
我试图验证的表单是动态加载的并添加到 div 中。
这是我添加方法并定义验证规则的地方。 addValMethod 在页面加载之后、attacheValidation 之前调用:
function addValMeth(){
$.validator.addMethod("uniqueUserName", function(value, element) {
$.get("databasescripts/validate_username.php", {username: value}, function(data2){
alert("Validate-> " + data2);
if(data2 == "1"){
alert("Validate 2");
return true;
}else if(data2 == "0"){
alert("Validate 3");
return false;
}
});
});
};
function attacheValidation($form){
$($form).validate({
rules: {
firstname: "required",// simple rule, converted to {required:true}
surname: "required",
address: "required",
zipcode: "required",
city: "required",
email: {
required: true,
email: true
},
username: {
uniqueUserName: true,
required: true
}
},
messages: {
firstname: "Förnamn saknas" ,
surname: "Efternamn saknas",
address: "Adress saknas",
zipcode: "Postkod saknas",
city: "Ort saknas",
companyname: "Företagsnamn saknas",
email: "Korrekt emailadress saknas",
username: {
uniqueUserName: "Användarnamnet är upptaget",
required: "Användarnamn saknas"
}
}
});
};
警报证明自定义验证器中的逻辑正常工作。所有其他验证规则都正常工作。
我不知道它是否可能相关,但每次返回真实结果时,都会请求自定义验证方法两次。当为 false 时,仅向该方法发出一次请求。
这是我想应用自定义验证方法的用户名字段。
I have created a custom validation rule (uniqueUserName). The problem I'm having is that the error message is allways presented for the custom rule, even when returning true from the custom validation method.
The form I am trying to validate is loaded dynamically and added to divs.
Here's where I add the method and defined the validation rules. The addValMethod is called after the page has been loaded but before attacheValidation:
function addValMeth(){
$.validator.addMethod("uniqueUserName", function(value, element) {
$.get("databasescripts/validate_username.php", {username: value}, function(data2){
alert("Validate-> " + data2);
if(data2 == "1"){
alert("Validate 2");
return true;
}else if(data2 == "0"){
alert("Validate 3");
return false;
}
});
});
};
function attacheValidation($form){
$($form).validate({
rules: {
firstname: "required",// simple rule, converted to {required:true}
surname: "required",
address: "required",
zipcode: "required",
city: "required",
email: {
required: true,
email: true
},
username: {
uniqueUserName: true,
required: true
}
},
messages: {
firstname: "Förnamn saknas" ,
surname: "Efternamn saknas",
address: "Adress saknas",
zipcode: "Postkod saknas",
city: "Ort saknas",
companyname: "Företagsnamn saknas",
email: "Korrekt emailadress saknas",
username: {
uniqueUserName: "Användarnamnet är upptaget",
required: "Användarnamn saknas"
}
}
});
};
The alerts proves the the logic in the custom validator works as it should. All other validation rules work properly.
I don't know if it could be related, but everytime true result is returned, the custom validation method is requested two times. When it is false, the request to the method is made only once.
It's the username field I want to apply the custom validation method on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
必须返回“true”和返回“false”。它需要一个字符串而不是布尔值。
One has to do return "true" and return "false". It expects a string not a boolean value.