Jump to content

Form is submitted even after returning false


piyusharora420

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.