Jquery Validation validate form-array
How can we validate the below code ? I want validate the input named software_name not be null. how to write the code? the validation rules looks only support single input.
<input name="software_name[]" type="text" class="input_txt_l software-name" value="" />
<input name="software_name[]" type="text" class="input_txt_l software-name" value="" />
<input name="software_name[]" type="text" class="input_txt_l software-name" value="" />
<input name="software_name[]" type="text" class="input_txt_l software-name" value="" />
============================update at 2011/07/26========================
I can't understand your help, I think I explain not clearly.try again.
I use this validation for form validate. http://bassistance.de/jquery-plugins/jquery-plugin-validation/
when we validate single input , we can use this to validate the name element. html code:
<form action="url/saved" method="post" id="server-form">
Name:<input name="name" type="text" class="input_txt" value=""/>
</form>
javascript code:
var v = $('#server-form').validate({
rules:{
name:{
required:true,
minlength:2,
maxlength:64
}
},
messages:{
name:{
required:'can not be null',
minlength:"too short",
maxlength:"too long"
}
},
onfocusout: function(element) {
if ( !this.checkable(element)) {
this.element(element);
}
},
errorPlacement: function(error, element) {
$(element).setValidateError({
err开发者_如何学GoorBox:error
});
},
success: function(element) {
$(element).setValidateOK();
}
});
but when we use form array. like this
<input name="software_name[]" type="text" class="input_txt_l software-name" value="" />
the jquery-validation-plugin looks not support . Am I right?
the only way looks likes @rharshath said ?
$('.software-name').each(function(i, $input) {
if( $.trim($input.val()) == "" ) {
// go nuts - alert it, or highlight that field,
// or put a tooltip beside it - whatever.
}
});
we have to hack it by myself ?
Follow the below steps.
1) Open jquery.validate.js file
2) Find checkForm: function() {
3) Replace this function with below code.
checkForm: function() {
this.prepareForm();
for ( var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++ ) {
if (this.findByName( elements[i].name ).length != undefined && this.findByName( elements[i].name ).length > 1) {
for (var cnt = 0; cnt < this.findByName( elements[i].name ).length; cnt++) {
this.check( this.findByName( elements[i].name )[cnt] );
}
} else {
this.check( elements[i] );
}
}
return this.valid();
}
Use: if($('.software-name[value!=""]').length==$('.software-name').length)
I know this is a long dead question but I ran into this issue today and came up with another way around the whole form_field[]
problem with jQuery validate.
What I was doing was echoing out multiple form fields using php. jQuery Validate's problem is that the names are the same. So I made the names unique, while still keeping their array behaviour, by adding the array index to the name, like so:
<?php for ($i=0; $i<10; $i++) { ?>
<input type='text' name='anArray[<?php echo $i;?>]' class='required'/>
<?php } ?>
<script>
$(function(){
$('form').validate();
});
</script>
Now it works like a charm - although advanced rules etc obviously would require another nasty loop or some such but I only need the required
part for my case. Hope this helps someone else.
like this:
$('.software-name').each(function(i, $input) {
if( $.trim($input.val()) == "" ) {
// go nuts - alert it, or highlight that field,
// or put a tooltip beside it - whatever.
}
});
If you validation plugin does not support what you want then you could either take its source code and modify it to have the feature you want, or you could write a full plugin for yourself (this is in "don't do that" territory).
I will often happen that plugins won't be your solution if what you're doing is something particularly different from what everyone is doing. That is okay, but you'll have to write more code and rely less on plugins.
No Pain No Gain.
精彩评论