0

I have array of textbox to collect 5 email address. I have used jQuery validate to validate the form to accept email addresses, unfortunately it's validating the first field only.

Though error message comes up for rest of the fields but it allows the form to submit. I need native solution for this issue.

Here is the code snippet:

$(document).ready(function(){
   $('#myform').validate({
       rules:{
          "foo[]":{
               required:true,
               email:true
                }
       },
       messages:{
          "foo[]":{
               required:"required",
               number:'only numbers!!!'
                }
            }
        });
    });

This is the form:

 <form method="post" action="" id="myform">
        <input type="text" name="foo[]" value="">
        <input type="text" name="foo[]" value="">
        <input type="text" name="foo[]" value="">
        <input type="text" name="foo[]" value="">
        <input type="text" name="foo[]" value="">
        <input type="image" name="bar">
    </form>
1

1 Answer 1

2

Here is the fix.You can use Jquery each function

$(document).ready(function(){
      $('#myform').validate({ // initialize the plugin
          // other options
      });

            $("[name^=foo]").each(function () {
            $(this).rules("add", {
                required: true,
                email:true,
                messages: {
                     required:"required"
                }
            });
        });
    });

And the form should looks like:

    <form method="post" action="" id="myform">
        <input type="text" name="foo[1]">
        <input type="text" name="foo[2]">
        <input type="text" name="foo[3]">
        <input type="text" name="foo[4]">
        <input type="text" name="foo[5]">
    </form>
Sign up to request clarification or add additional context in comments.

1 Comment

The most relevant part of this answer is the unique name attribute on each of the text input fields.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.