javascript - How to add IF ELSE condition for rules (for multiple forms) in Jquery validation -


i using jquery validation validate multiple forms same id. each form contains different fields , want add these fields validation rules jquery validate function. how can add these rules multiple forms (every form has same id)?

example

form1

<form id="new-form">     <input type="text" name="name" value=""/>     <input type="text" name="email" value=""/>     <input type="submit" name="submit" value="submit"/> </form> 

form 2

<form id="new-form">     <input type="text" name="mobile" value=""/>     <input type="text" name="address" value=""/>     <input type="submit" name="submit" value="submit"/> </form> 

javascript function

$('#new-form').validate({     // want add rules here both forms }); 

i using jquery validation validate multiple forms same id

  1. you cannot use same id multiple times on same page. it's invalid html , break javascript. first instance of id considered.

  2. if change id class, can freely use multiple times, , it's valid html.

    <form class="new-form">...</form> <form class="new-form">...</form> 

    however, jquery validate plugin still consider first instance (a limitation of particular plugin). workaround use jquery .each() class naming...

    $('.new-form').each(function() { // select every form on page class="new-form"     $(this).validate() { // initialize plugin on every selected form         // rules both forms     }); }); 

each form contains different fields , want add these fields validation rules jquery validate function.

simply declare rules both forms. plugin ignore field names not exist on form.

$('.new-form').each(function() { // select every form on page class="new-form"     $(this).validate() { // initialize plugin on every selected form         rules: {             name: {                 required: true             },             email: {                 required: true             },             mobile: {                 required: true             },             address: {                 required: true             }         },         // other options, etc.     }); }); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -