Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery验证程序验证具有相同名称的多个文本框只验证第一个输入_Jquery_Forms_Jquery Validate - Fatal编程技术网

使用jquery验证程序验证具有相同名称的多个文本框只验证第一个输入

使用jquery验证程序验证具有相同名称的多个文本框只验证第一个输入,jquery,forms,jquery-validate,Jquery,Forms,Jquery Validate,我试图用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>

我试图用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>

$(文档).ready(函数(){
$('#frm').validate();
$(“[name=inp_text]”)。每个(函数(){
$(此).rules(“添加”{
要求:正确,
checkValue:true
});   
});
$.validator.addMethod(“checkValue”,函数(值,元素){

var响应=((值>0)和&(值Yes),JQuery验证将只验证第一次出现。 文本框使用不同的名称。 或 您可以使用相同的名称但不同的ID。 并根据id申请支票

$(“#myform”)。验证({规则:{ #_字段的id_:{ 必需:true,检查值:true }}})

“我试图用jquery验证两个同名的输入 验证器…”

然后使用“从开始”选择器,
^=

$("[name^=inp_text]").each(function () {
    $(this).rules("add", {
        required: true,
        checkValue: true
    });
});
工作演示:

注意事项:在使用
规则('add')
方法时,您也可以通过
id
以元素为目标,但是在这种情况下,没有解决任何问题,因为插件在每个
输入
元素上仍然需要唯一的
名称。

朋友

为了验证文本框/复选框/选择相同的名称 使用以下方法,对我来说效果很好

这里的“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;
 }
函数validateCountry(){
inp=document.getElementsByTagName(“选择”);
对于(变量i=0;i
这里的“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;
 }
函数validateMinimumVal(){
inp=document.getElementsByTagName(“文本”);
对于(变量i=0;i
希望这有帮助!!
Jagan

添加如下类似规则或消息的配置

multipleInputSharedName: {
        inp_text: true
    }
更改jquery.validate.js中的代码 方法名称:元素

替换以下内容:

if ( this.name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
                return false;
            }
if ( (!validator.settings.multipleInputSharedName[this.name] && this.name in rulesCache) || !validator.objectLength( $( this ).rules() ) ) {
                return false;
            }
有以下几点

if ( this.name in rulesCache || !validator.objectLength( $( this ).rules() ) ) {
                return false;
            }
if ( (!validator.settings.multipleInputSharedName[this.name] && this.name in rulesCache) || !validator.objectLength( $( this ).rules() ) ) {
                return false;
            }

如果两个字段包含相同的名称,jQuery Validate插件将不起作用;只有第一个字段将进行验证。但是,在
.Validate()
中通过
id
声明规则是无效的,并且将完全破坏一切。请参阅:抱歉,亲爱的,您可以使用以下数组名称。$('#myform')。Validate({rules:{'data[]':{required:true,minlength:1}}}});非常感谢..你能解释一下为什么我使用inp_txt[]时它不起作用..我想这个名称应该是唯一的..或者我的假设是错误的..@dreamCoder,因为
inp_text[]
inp_text[]
完全相同。它如何“变得唯一”?我想我不完全理解你的要求。文档中没有
unique
这类东西。@OzanKurt,这是众所周知的,这个文档是缺乏的。但是,你可以自由地检查源代码,自己看看
name
属性是这个插件跟踪所有表单元素的方式正在验证。

如何执行此操作无法回答预期问题。