简体   繁体   中英

jQuery validate plugin validate inputs with similar names?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM