Pain Posted November 8, 2013 Share Posted November 8, 2013 (edited) Hi guys. Im trying to implement a javascript validation where if a customer leaves a blank select option, then an error pops out. I was able to display one error at a time even though all fields are blank. function validateSurvey2() { var err1 = document.getElementById('err1'); var err2 = document.getElementById('err2'); var err3 = document.getElementById('err3'); var err4 = document.getElementById('err4'); var question1 = document.survey.q1; var question2 = document.survey.q2; var question3 = document.survey.q3; var question4 = document.survey.q4; if(question1.selectedIndex === 0) { err1.style.display="inline"; question1.focus(); return false; } else { err1.style.display="none"; } if(question2.selectedIndex === 0) { err2.style.display="inline"; question2.focus(); return false; } else { err2.style.display="none"; } if(question3.selectedIndex === 0) { err3.style.display="inline"; question3.focus(); return false; } else { err3.style.display="none"; } if(question4.selectedIndex === 0) { err4.style.display="inline"; question4.focus(); return false; } else { err4.style.display="none"; } } This is probably because the script stops after the first 'return false'... I want every block of IF executed. thanks in advance for any help:) Edited November 8, 2013 by Pain Quote Link to comment https://forums.phpfreaks.com/topic/283737-multiple-validation/ Share on other sites More sharing options...
netaneledri Posted November 8, 2013 Share Posted November 8, 2013 function validateSurvey2() { var errors_count = 0; var err1 = document.getElementById('err1'); var err2 = document.getElementById('err2'); var err3 = document.getElementById('err3'); var err4 = document.getElementById('err4'); var question1 = document.survey.q1; var question2 = document.survey.q2; var question3 = document.survey.q3; var question4 = document.survey.q4; if(question1.selectedIndex === 0) { err1.style.display="inline"; question1.focus(); errors_count++; } else { err1.style.display="none"; } if(question2.selectedIndex === 0) { err2.style.display="inline"; question2.focus(); errors_count++; } else { err2.style.display="none"; } if(question3.selectedIndex === 0) { err3.style.display="inline"; question3.focus(); errors_count++; } else { err3.style.display="none"; } if(question4.selectedIndex === 0) { err4.style.display="inline"; question4.focus(); errors_count++; } else { err4.style.display="none"; } return errors_count == 0; } try that , I count every error and finaly return true / false by the errors count. Quote Link to comment https://forums.phpfreaks.com/topic/283737-multiple-validation/#findComment-1457610 Share on other sites More sharing options...
Pain Posted November 8, 2013 Author Share Posted November 8, 2013 Wow thanks, this really helped. However i do not completely understand how it works. Could you spare some time explaining it? Thanks so much netaneledri! Quote Link to comment https://forums.phpfreaks.com/topic/283737-multiple-validation/#findComment-1457613 Share on other sites More sharing options...
netaneledri Posted November 8, 2013 Share Posted November 8, 2013 Sure , let me explain : At the start I just set new var called errors_count , this var will count how many errors we have. Later , on each condition you wrote , I increase the errors_count if the condition return true and found an error. At the end I return (errors_count == 0) , its mean If there are no errors (what means errors_count = 0 like in start) we will return true and the form will be submitted but if errors_count > 0 we have errors so it will be return false because errors_count now equals 0. Quote Link to comment https://forums.phpfreaks.com/topic/283737-multiple-validation/#findComment-1457614 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.