1

I am using jQuery validate to check my form before submitting. In my HTML, I have inputs with similar names, ids. I am wondering how to validate inputs with similar names without writing out one by one (something like jQuery select elements with similar names)?

HTML
<form id="form1">
<table>
<tr><th>Input 1:</th><td><input type="text" name="input1" id="id_input1"/></td></tr>
<tr><th>Input 2:</th><td><input type="text" name="input2" id="id_input2"/></td></tr>
<tr><th>Input 3:</th><td><input type="text" name="input3" id="id_input3"/></td></tr>
</table>
</form>

Javascript

$("#form1").validate({
submitHandler:function(form) {
SubmittingForm()
},
rules: {                    //I am thinking if there has similar stuffs like
                            //$('th[name^="input"]')
input1: "required",     
input2:"required",
input3:"required",                      
    },
messages: {
    input1: "Please finish the table.",                 
    input2: "Please finish the table.",
        input3: "Please finish the table."  
          }
})
TTT
  • 4,354
  • 13
  • 73
  • 123

1 Answers1

1

For the validation part, you can only add a class="required" to your textfield and it will automatically validate it.

As for the messages, you can refer to this post

Community
  • 1
  • 1
VVV
  • 7,563
  • 3
  • 34
  • 55
  • thanks for the reply. But in my real code, I already have a class name filled with something else. – TTT Sep 04 '12 at 17:17
  • you can have more than 1 class... so you can still add class="whatever required" – VVV Sep 04 '12 at 17:24