I am trying to validate the two inputs having same name with jquery validator ..
<script src='
http://code.jquery.com/jquery-latest.min.js '></script>
<script src='http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js'></script>
<script>
$(document).ready(function(){
$('#frm').validate();
$("[name=inp_text]").each(function(){
$(this).rules("add", {
required: true,
checkValue: true
});
});
$.validator.addMethod("checkValue", function(value, element) {
var response = ((value > 0)&&(value <= 100))||((value == 'test1')||(value =='test2'));
return response;
}, "invalid value");
})
</script>
<form id='frm' method='post'>
<input type='text' name='inp_text'/><br>
<input type='text' name='inp_text'/><br>
<input type='submit' name=''/>
</frm>
but when I run this code it only validates first inputs and second inputs gets simply ignored
I have also tried using <input type='text' name='inp_text[]'/><br>
but still not working.
what should I change...?
Thanks in advance
Yes, JQuery validate will only validate first occurrence. You use different names for textboxes. OR You can use same names but different ids. and apply check according to id.
You cannot have two
input
fields oftype="text"
with the samename
or this plugin will not work properly. Thename
attribute must be unique. (One exception to thename
being unique, is that "groupings" of checkbox or radio inputs will share the samename
as the corresponding submission is a single point of data. However, thename
must still be unique to each grouping of checkbox and radio elements.)Make each
name
attribute unique.Then use the "starts with" selector,
^=
...Working DEMO: http://jsfiddle.net/PgLh3/
NOTES: You can also target elements by
id
when using therules('add')
method, however for this case, nothing is solved because the plugin still requires a uniquename
on eachinput
element.Add a configuration like the following like rules or messages
Change code in jquery.validate.js Method name: elements
replace the following
with the following
Friends!
In order to validate the text box/checkbox/select with the same name use the following it is working fine for me.
Here 'ctryId' is the name of the select box.
Here 'minVal' is the name of the text box
Hope this helps!! Jagan