Click here to Skip to main content
15,886,745 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a form with some validation that prevents the user from submitting the form with empty input boxes. However, when the form submit button is clicked, the error message is displayed but then the form is still processed.

This is the code I have:

JavaScript
function functionName() {
	var var1 = document.getElementById('id1').value;
	var var2 = document.getElementById('id2').value;
	var var3= document.getElementById('id3').selectedIndex;
	var var4= document.getElementById('id4').value;
	if ((var3 === 1) &&(var1.length < 2) || (var2.length < 2)){
        alert("");	
        var1.style.borderColor = "red";
	var2.style.borderColor = "red";
	return false;
	}
	else if ((var3 === 2) && (var4.length < 2)){
        alert("");
	var4.style.borderColor = "red";
	return false;
	}
	else {
	var4.style.borderColor = "green";
	var1.style.borderColor = "green";
	var2.style.borderColor = "green";
	return true;
	}
	}
Posted
Comments
PIEBALDconsult 21-Apr-13 13:23pm    
The Submit button shouldn't be Enabled until all the requirements are satisfied.
I suspect you are not doing anything with the result of this method.
Step through with the debugger and see what happens.
Hemant Singh Rautela 21-Apr-13 13:24pm    
what do you mean by form processed....(this fnction is return true or false thats it not other processing...)
Prasad Khandekar 21-Apr-13 13:32pm    
Do you call this function on onclick of submit button? Try calling this function from form's onsubmit handler.
Mike Meinz 21-Apr-13 18:32pm    
This may or may not be the cause of the problem, but sometimes operator precedence is a cause of problems when compound if statements are used.

For this line of code, are you sure that the order of precedence of the AND and OR operators is what you want?
if ((var3 === 1) && (var1.length < 2) || (var2.length < 2)){


Based on the C# Operators chart in the documentation, the following is how that line of code would be interpreted using parenthesis to signify the precedence:
if ( ( (var3 === 1) && (var1.length < 2) ) || (var2.length < 2)){
*********************************** || ***************

My guess is that you really intended this:
if ( (var3 === 1) && ( (var1.length < 2) || (var2.length < 2) ) ){
********** && ****************************************

1 solution

How actually you are making call to this function ??

It has to be either of these -
HTML
<form method="post" onsubmit="return functionName()">
...
<input type="submit" value="Submit">
</input></form>


or (inline or within the function invoked on click of the button)

HTML
<form method="post">
...
<input type="submit" value="Submit" onclick="return functionName()">
</input></form>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900