How do you create rules for jquery form validate p

2019-06-19 11:15发布

Does anyone know how to create the rules for the jquery form validate plugin when you have a name attribute that is an array?

eg.

<form id="myForm">
<input type="checkbox" name="data[]" id="firstId" value="1" />One <br />
<input type="checkbox" name="data[]" id="secondId" value="2" />One <br />

...

I'm trying to attach a rule to this checkbox using the validate plugin syntax

$('#myform').validate({
  rules : {
    data: { required: true, minlength: 1 
    }
  }
}
);

Problem is that the 'data' syntax is incorrect. Using data[] or data\[\] is also invalid. I've only gotten it to work with

$('#firstId').rules('add', { required: true, minlength: 1});

Anyone have a suggestion?

2条回答
三岁会撩人
2楼-- · 2019-06-19 11:25

Have you tried:

$('#myform').validate({
  rules : {
    'data[]': { required: true, minlength: 1 
    }
  }
}
);

I don't know if it will work, give it a shot.

查看更多
神经病院院长
3楼-- · 2019-06-19 11:39

You need to wrap the name of the input (in this case data[]) in quotes

$('#myform').validate({
  rules : {
    'data[]': { required: true, minlength: 1 }
  }
});

see the documentation here for Field with complex names (Brackets Dots): http://docs.jquery.com/Plugins/Validation/Reference#Fields_with_complex_names_.28brackets.2C_dots.29

查看更多
登录 后发表回答