jQuery validation of a form name with square brackets

一笑奈何 提交于 2019-12-09 00:32:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!