问题
I have this form with inputs with the same name but similar (incremental) ids.
I want the form to validate if there is a name on person, the age must be mandatory..
What happens now is that only the first input is mandatory.
Here is my code:
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
</head>
<body>
<form id="people">
<div class="section">
<input id="person1" name="person" class="person" type="text" placeholder="First Name" />
<input id="age1" name="age" class="age" type="text" placeholder="Age" />
</div>
<div class="section">
<input id="person2" name="person" class="person" type="text" placeholder="First Name" />
<input id="age2" name="age" class="age" type="text" placeholder="Age" />
</div>
<div class="section">
<input id="person3" name="person" class="person" type="text" placeholder="First Name" />
<input id="age3" name="age" class="age" type="text" placeholder="Age" />
</div>
...
<input type="submit" id="submit" name="submit" value="Add" />
</form>
<script type="text/javascript">
$(function(){
$('#people').validate();
$('#submit').click(function(){
$('[id^="person"]').each(function(){
if ($(this).val().length>0){
//alert($(this).val());
//alert($(this).parent().find('.age').val());
$(this).rules('add', {
required: true,
minlength: 2,
messages: {
required: "Specify the person name",
minlength: "Minimum of 2 characters"
}
});
$(this).parent().find('.age').rules('add', {
required: true,
number: true,
messages: {
required: "Must have an age",
number: "Specify a valid age"
}
});
}
});
});
});
</script>
</body>
回答1:
The jQuery Validator plugin doesn't support multiple fields with the same name out-of-the-box. You'll need to edit the source of the plugin to check the fields the way you want.
See this answer to a similar question for the work-around.
回答2:
Friends!
In order to validate the text box with the same name use the following it is working fine for me. in addition, No need to declare input parameter as arrays.
Here 'minVal' is the name of the text box. similarly, for dropdown use 'document.getElementsByTagName("select");'
function validateMinimumVal(){
inp = document.getElementsByTagName("TEXT");
for ( var i = 0; i < inp.length; ++i ) {
if (inp[i].name =="minVal" && inp[i].value==''){
alert('plesae Add a MINIMUM VALUE!!');
return false;
}
}
return true;
}
Hope this helps!! Jagan
回答3:
<div class="section">
<input id="person1" name="person[]" class="person" type="text" placeholder="First Name" />
<input id="age1" name="age[]" class="age" type="text" placeholder="Age" />
</div>
<div class="section">
<input id="person2" name="person[]" class="person" type="text" placeholder="First Name" />
<input id="age2" name="age[]" class="age" type="text" placeholder="Age" />
</div>
<div class="section">
<input id="person3" name="person[]" class="person" type="text" placeholder="First Name" />
<input id="age3" name="age[]" class="age" type="text" placeholder="Age" />
</div>
You should add square brackets and loop through the array.
来源:https://stackoverflow.com/questions/9033184/jquery-validate-multiple-fields-with-the-same-name