JQuery.Validate 添加带有消息的动态规则
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15482523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery.Validate adding dynamic rules with messages
提问by Martin Robins
I have a form on which I am using jquery.validate. I initially call validate with a set of rules and custom messages...
我有一个使用 jquery.validate 的表单。我最初使用一组规则和自定义消息调用验证...
$("#formName").validate( {
rules: {
myExistingInput: {
required: true
}
},
messages: {
myExistingInput: {
required: "Enter something"
}
},
ignore: null, // include hidden fields (see below)
submitHandler: function(form) {
// do stuff
},
invalidHandler: function(event, validator) {
// do stuff (some of the fields may have been hidden by a collapsible panel
// if there is an error on one of those fields, expand the panel so the error
// becomes visible)
}
});
Later, I dynamically add fields to the form, and add rules for those fields too...
后来,我动态地向表单添加字段,并为这些字段添加规则......
$("#formName").append(...);
$("#newInputName").rules("add", {
required: true,
messages: {
required: "Enter something else"
}
});
If I then submit the form, I get an error from within jquery.validate...
如果我然后提交表单,我会从 jquery.validate 中收到一个错误...
Exception occured when checking element newInputName, check the 'messages' method.TypeError: Unable to get property 'call' of undefined or null reference
检查元素 newInputName 时发生异常,检查“消息”方法。类型错误:无法获取未定义或空引用的属性“调用”
Debugging in the browser, I can see the error is being thrown from within the "check" function, and that the "method" variable is set to "messages".
在浏览器中调试,我可以看到错误是从“check”函数中抛出的,并且“method”变量设置为“messages”。
If I remove the messages from the call to rules("add",...
如果我从对 rules("add",...
$("#newInputName").rules("add", {
required: true
});
it works as expected, but obviously I now have no custom error messages.
它按预期工作,但显然我现在没有自定义错误消息。
I have seen many examples here on SO indicating that my syntax is correct. Any suggestions?
我在 SO 上看到了很多例子,表明我的语法是正确的。有什么建议?
BTW: jQuery Validation Plugin - v1.11.0 - 2/4/2013
顺便说一句:jQuery 验证插件 - v1.11.0 - 2/4/2013
回答by Sparky
Your code seems to be working, without error, as you posted it.
正如您发布的那样,您的代码似乎可以正常工作,没有错误。
DEMO with DOM ready: http://jsfiddle.net/UZTnE/
已准备好 DOM 的演示:http: //jsfiddle.net/UZTnE/
DEMO with PageInit & jQuery Mobile: http://jsfiddle.net/xJ3E2/
带有 PageInit 和 jQuery Mobile 的演示:http: //jsfiddle.net/xJ3E2/
$(document).on("pageinit", function () {
$('#myform').validate({ // initialize the plugin
rules: {
field1: {
required: true
}
},
messages: {
field1: {
required: "Enter something"
}
}
});
$('[name*="field"]').each(function () {
$(this).rules('add', {
required: true,
messages: {
required: "Enter something else"
}
});
});
});
HTML:
HTML:
<form id="myform">
<input type="text" name="field1" />
<input type="text" name="field2" />
<input type="submit" />
</form>
BTW:
顺便说一句:
this...
这个...
ignore: null, // include hidden fields
should be...
应该...
ignore: [], // include hidden fields
回答by hardik prajapati
$(document).ready(function(){
$("#controlId").rules("add", {
required : true,
messages : { required : 'field is required.' }
});
});
回答by Eagle_one
as an answer to this old issue, I do like this to get the meassges inside the rules object.
作为对这个老问题的回答,我确实喜欢这样来获取规则对象中的消息。
After you have added the rules you can add messages like this:
添加规则后,您可以添加如下消息:
var inputRules = $('input').rules();
inputRules.messages = {required: 'your message'};
Good luck!
祝你好运!