javascript - validation for multiple forms generated by loop? -


i have loop generates 5 forms.there lot of animation involved , 1 form displayed @ time.

my link

i have done validation id based on other page(working fine). here situation different,i cannot generate id because of loop.so entire form validation class based.

myquestion: how manage single function validates 5 forms.

if interested in id based code-> thefiddle (not working),just basic code

my code:for simplicity

for(i=0;i<=5;i++){ //html of form in fiddle. } 

new jquery.i understand $(this) concept in jquery.

hope clear.no plugin plz.

first things first, if element not unique not use id it.

let's have 2 forms here:

<form id="formone" class="validform" method="post">     <input class="email" type="text"/>     <input class="newsletter" type="checkbox"/>     <input type="submit" value="send"/>     <p class="error"></p> </form>  <form id="formtwo" class="validform" method="post">     <input class="email" type="text"/>     <input class="newsletter" type="checkbox"/>     <input type="submit" value="send"/>     <p class="error"></p> </form> 

it's important know form elements parents of input or select elements. when use $(this) can refer parent.

to validate in jquery:

$('.validform').submit(function(event){     var allinputsarevalid = true;     var form = null;     $('.validform input').each(function(){         switch($(this).attr('class')){             case 'email':                 if($(this).val() == "") {                     allinputsarevalid = false;                 }             break;             case 'newsletter':                 // optional?             break;         }         if(!allinputsarevalid) {             form = $(this).parent();             break;         }     });     if(allinputsarevalid){       // valid, transfer data     } else {         event.preventdefault();         $(form).children('.error').text(errormessage);     } }); 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -