jQuery验证验证表单数组 [英] Jquery Validation validate form-array
问题描述
我们如何验证以下代码? 我想验证名为software_name的输入不为null.怎么写代码? 验证规则看起来仅支持单个输入.
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="" />
============================更新于2011/07/26 ============ ===========
============================update at 2011/07/26========================
我无法理解您的帮助,我想我解释不清楚,请重试.
I can't understand your help, I think I explain not clearly.try again.
我将此验证用于表单验证. http://bassistance.de/jquery-plugins/jquery-plugin-validation/
I use this validation for form validate. http://bassistance.de/jquery-plugins/jquery-plugin-validation/
当我们验证单个输入时,我们可以使用它来验证name元素. html代码:
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代码:
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({
errorBox: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="" />
jquery-validation-plugin似乎不支持.我说的对吗?
the jquery-validation-plugin looks not support . Am I right?
唯一的方法就像@rharshath说的那样?
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.
}
});
我们必须自己破解吗?
推荐答案
请按照以下步骤操作.
Follow the below steps.
1)打开jquery.validate.js文件
2)查找checkForm:function(){
3)用下面的代码替换此功能.
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();
}
这篇关于jQuery验证验证表单数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!