piyusharora420 Posted June 13, 2012 Share Posted June 13, 2012 Hi, I have done some validations using javascript. It is working but form is still submitted even it returns false. I have used onclick="return function()" in html file. function isValidName(name,nameRegex) { if(!(name.match(nameRegex))) { alert("name can contains only alphabets"); //name.focus(); return false; } } //email format should match with the emailRegex function isValidEmail(email,emailRegex) { if(!email.match(emailRegex)) { alert("invalid email address"); //email.focus(); return false; } } //Password Length should be b/w 8 and 15 function isValidPassword(password,passRegex) { if(!password.match(passRegex)) { alert("Invalid password"); //password.focus(); return false; } } function checkInput() { // Stored values in variables var firstName=document.getElementById("firstName").value; var lastName=document.getElementById("lastName").value; var email=document.getElementById("email").value.toLowerCase(); var reEmail=document.getElementById("reEmail").value.toLowerCase(); var password=document.getElementById("password").value; var emailRegex=/^[a-zA-Z\_][\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/; var nameRegex=/^[a-zA-Z]+$/; var passRegex=/^[\w\-\@\#\&\*\%\$]{8,15}$/; isValidName(firstName,nameRegex); isValidName(lastName,nameRegex); isValidPassword(password,passRegex); isValidEmail(email,emailRegex); isValidEmail(reEmail,emailRegex); if(document.getElementById('gender').value=="") { alert("please select your gender"); document.getElementById('gender').focus(); return false; } return true; } Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/264089-form-is-submitted-even-after-returning-false/ Share on other sites More sharing options...
requinix Posted June 13, 2012 Share Posted June 13, 2012 return only returns from the current function. It doesn't propagate up the call stack or something. With your checkInput() as it is now, the isValid* functions can return whatever they want and it won't do anything because checkInput() doesn't look at their return values. So make it look at their return values: if any of them return false then checkInput() should return false too. Quote Link to comment https://forums.phpfreaks.com/topic/264089-form-is-submitted-even-after-returning-false/#findComment-1353403 Share on other sites More sharing options...
piyusharora420 Posted June 13, 2012 Author Share Posted June 13, 2012 You are correct. It worked. Thanks alot Quote Link to comment https://forums.phpfreaks.com/topic/264089-form-is-submitted-even-after-returning-false/#findComment-1353422 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.