validating multiple textbox having same name with

2019-01-04 13:50发布

I am trying to validate the two inputs having same name with jquery validator ..

<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.

what should I change...?

Thanks in advance

4条回答
甜甜的少女心
2楼-- · 2019-01-04 14:20

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.

$("#myform").validate({ rules: { #id_of_field: { required: true, checkValue: true } } });

查看更多
男人必须洒脱
3楼-- · 2019-01-04 14:24

"I am trying to validate the two inputs having same name with jquery validator ..."

<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 must be 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.)

"what should I change...?"

Make each name attribute unique.

<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/

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 unique name on each input element.

查看更多
家丑人穷心不美
4楼-- · 2019-01-04 14:31

Add a configuration like the following like rules or messages

multipleInputSharedName: {
        inp_text: true
    }

Change code in jquery.validate.js Method name: elements

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;
            }
查看更多
\"骚年 ilove
5楼-- · 2019-01-04 14:33

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.

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

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

查看更多
登录 后发表回答