jQuery:表单验证插件-验证特定的字符串? [英] JQuery: Form Validation plugin - validate specific string?
本文介绍了jQuery:表单验证插件-验证特定的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个使用此插件的表单:
I have a form that uses this plugin:
http://bassistance.de/jquery-plugins/jquery-plugin-validation/
效果很好-但我无法弄清楚如何通过输入到文本字段中的特定字符串进行验证.例如,我要创建一个仅在用户输入为"foo"时才验证字段的规则.
Works great - but I cant figure how to validate by a specific string in-putted into a text field. For example, I want to create a rule that only validates a field if the users input is "foo".
此处提供文档 http://docs.jquery.com/Plugins/Validation#API_Documentation
但是他们似乎都不是我想要的那个.有人知道解决这个问题的方法吗?
But none of them seem to be the one I want. Does anyone know a way to get round this?
谢谢!
推荐答案
简单的一个...
$.validator.addMethod("equals", function(value, element, string) {
return value === string;
}, $.validator.format("Please enter '{0}'"));
$("#form").validate({
rules: {
name: {
required: true,
equals: "foo"
}
}
});
根据您的要求更新
$.validator.addMethod("equals", function(value, element, string) {
return $.inArray(value, string) !== -1;
}, $.validator.format("Please enter '{0}'"));
$("#form").validate({
rules: {
name: {
required: true,
equals: ["foo", "bar", 'blah"]
}
},
messages: {
name: "Please enter either '{0}' or '{1}' or '{2}'"
}
});
这篇关于jQuery:表单验证插件-验证特定的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文