jQuery Validation Form远程规则,成功消息 [英] jQuery Validation Form Remote rule, success message
问题描述
我正在对我的注册表使用jquery验证,它可以正常工作,但是我遇到了问题.我检查电子邮件是否存在,如果确实存在,则收到错误消息.现在,我想对此进行编辑,因此,如果电子邮件可以免费使用.错误消息将更改为:此电子邮件可免费使用.
I am using jquery validation for my register form, it works perfectly but I'm running into a problem. I check if the email exists, if an email does exists I'm receiving an error message. Now I would like to edit this, so, if the email is free to use. The error message will change to: This email is free to use.
$(document).ready(function(){
$("#registratieform").validate({
rules: {
email: {
required: true,
email: true,
remote: {
url: "includes/check_email.php",
type: "post",
complete: function(data){
if( data.responseText == "false" ) {
alert("Free");
}
}
},
},
},
messages: {
email: {
required: "This field is required",
email: "Please enter a valid email address",
remote: jQuery.format("{0} is already taken")
},
},
});
});
警报有效,但是此消息必须出现在错误所在的标签中.这可能吗?
The Alert works, but this message has to appear in the label where the errors are. Is this possible?
推荐答案
我找到了解决我们问题的方法,花了一天的时间解决所有现有的解决方案,但没人满意我,并且学习了jqvalidator的一些源代码,我发现它很容易实现
I`v found solution for our problem, a spend a day to all existed solution but noone satisfied me and learn a bit source code of jqvalidator I found that it is easy to realize it
$("#myform").validate({
rules: {
somealiasname: {
required: true,
remote: {
url: "www.callthisurl.com/remote",
type: "GET",
success: function (data) {// Here we got an array of elements for example
var result = true,
validator = $("#myform").data("validator"), //here we get the validator for current form. We shure that validator is got because during initialization step the form had stored validator once.
element = $("#myform").find("input[name=somealiasname]"),
currentAlias = element.val(),
previous, errors, message, submitted;
element = element[0];
previous = validator.previousValue(element); // here we get the cached value of element in jqvalidation system
data.forEach(function (it) {//here we check if all alias is uniq for example
result = !result ? false : it.alias != currentAlias;
});
validator.settings.messages[element.name].remote = previous.originalMessage; // this code I found in the source code of validator (line 1339)
if (result) {
submitted = validator.formSubmitted;
validator.prepareElement(element);
validator.formSubmitted = submitted;
validator.successList.push(element);
delete validator.invalid[element.name];
validator.showErrors();
} else {
errors = {};
message = validator.defaultMessage(element, "remote");
errors[element.name] = previous.message = $.isFunction(message) ? message(value) : message;
validator.invalid[element.name] = true;
validator.showErrors(errors);
}
previous.valid = result;
validator.stopRequest(element, result);
}.bind(this)
}
}
}
```
tadam-一切都很完美!
tadam - everything is perfectly!
此代码已通过jqvalidation 1.14.0进行了测试
This code has been tested with jqvalidation 1.14.0
我希望我能帮助别人
这篇关于jQuery Validation Form远程规则,成功消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!