Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validation and array like named input

How can I refer to the second input in this jsfiddle while defining validation rules? I am thinking at something like "postArray.inputOne" instead of inputOne, as I do for the first one. Of course, it doesn't work. Is there any way I can do it?

My HTML looks like this:

<form id="formId" method="post" action="">
    <input type="text" name="inputOne" />
    <input type="text" name="postArray[inputOne]" />
</form>​

The validation rules look like this:

$("#formId").validate({
    rules: {
      inputOne: {
        required: true
      }
    },
    messages: {
      inputOne: {
        required: "Required!"
      }
    }
});    

Thanks!

like image 796
Michael Avatar asked Jul 07 '26 18:07

Michael


1 Answers

You can validate any text field by name

    $(document).ready(function () {

    $("#formId").validate({
        rules:{
            "inputOne":{
                required:true
            },

            "postArray[inputOne]":{
                required:true
            },

            messages:{
                "inputOne":"This field is required",
                "postArray[inputOne]":"This field is required"
            }
        }
    });
});

Hope it will help.....

like image 191
Kashiftufail Avatar answered Jul 14 '26 07:07

Kashiftufail