8

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 Answers 4

18

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

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks alot .. can you explain me why it was not working when I use inp_txt[] .... whith that name should become unique I suppose.. or am I wrong with this assumption .. ?
@dreamCoder, because inp_text[] is exactly the same as inp_text[]. How would it "become unique"? I guess I don't fully understand what you're asking.
There is no such thing as unique in the documentation.
@OzanKurt, it's fairly well known that this documentation is lacking. However, you are free to examine the source code and see for yourself that the name attribute is how this plugin keeps track of all form elements being validated. Also see this demo where the plugin fails when the name attribute is duplicated: jsfiddle.net/ed3vxgmy/
<form id="myform"> <input type="text" name="field[1]lang[1][name]" /> <br/> <input type="text" name="field[2]lang[2][name]" /> <br/> <input type="submit" /> </form> How to do
1

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;
            }

Comments

-1

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 } } });

2 Comments

True, the jQuery Validate plugin will not not work when two fields to contain the same name; only the first field will validate. However, declaring rules by id within .validate() is not valid and will absolutely break everything. See: jsfiddle.net/tpg7M
Sorry dear, you can use following for array names.$('#myform').validate({ rules : { 'data[]': { required: true, minlength: 1 } } });
-1

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

1 Comment

This doesn't answer the intended question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.