Need some help with validation with a form? I have several radio buttons on my form along with text areas. What I need to do is make sure if the “NO” radio button is marked they need to provide a comment to that question.
I have come up with this to see if the radio button is marked for each question how would incorporate the comment areas.
Code:function ValidateAuditSurvey () { var q=0; var name; var prevName=0; var radioCount=0; for (var i=0;i<document.forms(0).elements.length;i++){ if(document.forms(0).elements[i].type=='radio'){ radioCount++ if (prevName==0){ //alert('first time set prevName'); prevName=document.forms(0).elements[i].name; } name = document.forms(0).elements[i].name; if (prevName!=name){ //alert('name changed, test q'); if (q==0){ //alert('non found'); break; } else{ //alert('found one checked'); } q=0; if(document.forms(0).elements[i].checked){ //alert('found a checked radio, add one to q') q++; } } else{ //name is same if(document.forms(0).elements[i].checked){ //alert('found a checked radio, add one to q') q++; } } //alert(q+'-'+name+'-'+prevName) prevName = document.forms(0).elements[i].name; } } if (q==0 && radioCount!=0){ alert('Please select an answer for each question.'); return false; } else{ document.forms[0].submit() return true; } }



Reply With Quote
Bookmarks