使用 jquery 验证器验证多个具有相同名称的文本框仅验证第一个输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15739390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
validating multiple textbox having same name with jquery validator validates only first input
提问by alwaysLearn
I am trying to validate the two inputs having same name with jquery validator ..
我正在尝试使用 jquery 验证器验证具有相同名称的两个输入..
<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.
我也尝试过使用<input type='text' name='inp_text[]'/><br>
但仍然无法正常工作。
what should I change...?
我应该改变什么...?
Thanks in advance
提前致谢
回答by Sparky
"I am trying to validate the two inputs having same name with jquery validator ..."
“我正在尝试使用 jquery 验证器验证具有相同名称的两个输入......”
<input type='text' name='inp_text'/>
<input type='text' name='inp_text'/>
"but when I run this code it only validates first inputs and second inputs gets simply ignored"
“但是当我运行这段代码时,它只验证第一个输入,而第二个输入被简单地忽略”
You cannot have two input
fields of type="text"
with the same name
or this plugin will not work properly. The name
attribute mustbe unique. (One exception to the name
being unique, is that "groupings" of checkbox or radio inputs will share the same name
as the corresponding submission is a single point of data. However, the name
must still be unique to each grouping of checkbox and radio elements.)
您不能有两个相同的input
字段,否则此插件将无法正常工作。该属性必须是唯一的。 (唯一的一个例外是复选框或单选输入的“分组”将与相应的提交共享相同的数据点。但是,复选框和单选元素的每个分组仍然必须是唯一的。)type="text"
name
name
name
name
name
"what should I change...?"
“我该换什么……?”
Make each name
attribute unique.
使每个name
属性都独一无二。
<input type='text' name='inp_text[1]'/>
<input type='text' name='inp_text[2]'/>
Then use the "starts with" selector, ^=
...
然后使用“开始于”选择器,^=
...
$("[name^=inp_text]").each(function () {
$(this).rules("add", {
required: true,
checkValue: true
});
});
Working DEMO: http://jsfiddle.net/PgLh3/
工作演示:http: //jsfiddle.net/PgLh3/
NOTES: You can also target elements by id
when using the rules('add')
method, however for this case, nothing is solved because the plugin still requires a uniquename
on each input
element.
注意:您也可以id
在使用该rules('add')
方法时定位元素,但是对于这种情况,没有解决任何问题,因为插件仍然需要每个元素的唯一性。name
input
回答by Manas Kumar Das
Add a configuration like the following like rules or messages
添加如下配置,如规则或消息
multipleInputSharedName: {
inp_text: true
}
Change code in jquery.validate.js Method name: elements
更改 jquery.validate.js 中的代码方法名称:元素
replace the following
替换以下内容
if ( this.name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
return false;
}
with the following
与以下
if ( (!validator.settings.multipleInputSharedName[this.name] && this.name in rulesCache) || !validator.objectLength( $( this ).rules() ) ) {
return false;
}
回答by Jaskaran Singh
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.
是的,JQuery 验证只会验证第一次出现。您对文本框使用不同的名称。或 您可以使用相同的名称但不同的 ID。并根据id申请检查。
$("#myform").validate({ rules: { #id_of_field: { required: true, checkValue: true } } });
$("#myform").validate({ rules: { #id_of_field: { required: true, checkValue: true } } });
回答by Jagan
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.
这里的 'ctryId' 是选择框的名称。
function validateCountry(){
inp = document.getElementsByTagName("select");
for ( var i = 0; i < inp.length; ++i ) {
if (inp[i].name =="ctryId" && inp[i].value==''){
alert('plesae select a country!');
return false;
}
}
return true;
}
Here 'minVal' is the name of the text box
这里'minVal'是文本框的名称
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
希望这可以帮助!!贾根