在name属性中带有前缀的字段上的jQuery验证 [英] jQuery Validation on field(s) with a prefix in the name property
问题描述
我正在尝试将jQuery Validation插件添加到某些网站,但是我遇到了一个问题,因为它应该验证的字段在name属性上带有前缀,例如"Customer.FirstName". .
I'm trying to add the jQuery Validation plugin to some websites and I'm running into a bit of an issue in that the fields that it's supposed to validate have a prefix on the name property, such as "Customer.FirstName".
由于您必须将JSON对象传递给规则的validate函数,因此它无法工作,因为它永远找不到元素.
Since you have to pass a JSON object to the validate function for the rules, it doesn't work because it never finds the elements.
是否有一种方法可以按字段进行操作,还是仍可以传入将字段ID指定为字符串的JSON对象的变体,例如"#Customer \ .FirstName"?
Is there a way to do per field basis, or can I still pass in a variant of the JSON object that specifies the field id as a string, such as "#Customer\.FirstName"?
提前谢谢!
根据Greg的建议,我可以使用它.因此,对于遇到此类问题的任何人,您都必须这样做:
Per Greg's suggestions, I got it to work. So for anyone who has issues like these, you have to do it like this:
$("form").validate({
rules: {
"Prefix.FieldName": "validationKeyword"
}
});
请勿在选择器中添加#",也请勿在选择器中添加"\\"转义字符.因此,#Prefix \\.FieldName"将不匹配任何内容,因此,请忽略这些字符.
DO NOT add the "#" to the selector, and DO NOT add the "\\" escape chars to the selector. So, "#Prefix\\.FieldName" will not match anything, so just leave those chars out.
推荐答案
JSON支持带有."的键.在其中-只需引用它们即可:
JSON supports keys with "." in them - just quote them:
var obj = {"#Customer.FirstName": "value"};
实际上,要使用正确的JSON,应始终将其用双引号引起来.
In fact to be proper JSON they should always be double-quoted.
如果这是一个选择器,则可以转义.像这样:"#Customer\\.FirstName"
if this is a selector then you can escape the . like this: "#Customer\\.FirstName"
Edit2:文档说密钥必须是一个名称,所以我应该将其命名为"Customer.Firstname"
或"Customer \ .Firstname",具体取决于插件的编码方式.您需要<input name="Customer.Firstname" ...>
.
The docs say the key needs to be a name so I it should either be "Customer.Firstname"
or "Customer\.Firstname" depending on how well-coded the plugin is. You'll need <input name="Customer.Firstname" ...>
.
这篇关于在name属性中带有前缀的字段上的jQuery验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!