Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

forms - validating multiple textbox having same name with jquery validator validates only first input

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...