问题
I have the following validation code for a text field but the 'field' below needs to be replaced with the name of the text field, which is created dynamically.
My problem is it has a square bracket in the name - options[483998]
How would I go about adding this to the code below as obviously if I do a direct replace of field with options[483998] it creates invalid coding.
jQuery("#product_addtocart_form").validate({
rules: {
field: {
required: true,
range: [100, 2540]
}
}
});
回答1:
Demo - http://jsfiddle.net/sY82j/
jQuery("#product_addtocart_form").validate({
rules: {
"options[483998]": {
required: true,
range: [100, 2540]
}
}
});
回答2:
Just put it in quotes: "options[483998]"
回答3:
Aren't you able to pull this off?
jQuery("#product_addtocart_form").validate({
rules: {
'options[483998]': {
required: true,
range: [100, 2540]
}
}
});
回答4:
I've tried this as well, but my validation API code wont work with this technique.
"upload_images[]":
{
required: true,
accept:"jpg,png,jpeg"
}
I'm working with a image upload input with multiple images, i need it to check if anything was selected before submitting the form. The other inputs work perfectly with the validation API.
来源:https://stackoverflow.com/questions/7992734/jquery-validation-of-a-form-name-with-square-brackets