Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

form validation

Status
Not open for further replies.

spruce009

MIS
May 12, 2003
33
US
I am using the following code to validate that certain form elements are not left blank, but it does not work. The alert pops up, but then instead of setting focus back on the form, it goes right to the forms action. Any ideas? Thanks.

function CheckRequired(){
var returnValue=false;
if (document.PoEntry.Shipping.value =="")
{
alert ("Shipping cost is a required field, please enter a value and try again.");
document.PoEntry.Shipping.focus();
}
if (document.PoEntry.Tax.value =="")
{
alert ("Tax is a required field. Please enter a value and try again.");
document.PoEntry.Tax.focus();
}
if (document.PoEntry.RecAttn.value =="")
{
alert ("The Attention field is a required field. Please enter a value and try again.");
document.PoEntry.RecAttn.focus();
}
if (document.PoEntry.txtReasonFor.value =="")
{
alert ("Please enter a reason for this request and try again.");
document.PoEntry.txtReasonFor.focus();
}
else
{
returnValue=true;
}
return returnValue;
}
 
Probably this:

Code:
<form blah blah... onsubmit="[b]return[/b] checkRequired();">
 
Can you post the code where you call this function? Also have you put in a message box at the end to see what returnValue is being returned?

Either its somehow returning a true value when it should be false or you're calling the function on the page submit but not specifying that it should only run if a true value is returned.

- VB Rookie
 

Change your "if" statments to be "else if" statements:

Code:
function CheckRequired() {
	var returnValue=false;
	if (document.PoEntry.Shipping.value =="")
	{
		alert ("Shipping cost is a required field, please enter a value and try again.");
		document.PoEntry.Shipping.focus();
	}
	else if (document.PoEntry.Tax.value =="")
	{
		alert ("Tax is a required field.  Please enter a value and try again.");
		document.PoEntry.Tax.focus();
	}
	else if (document.PoEntry.RecAttn.value =="")
	{
		alert ("The Attention field is a required field.  Please enter a value and try again.");
		document.PoEntry.RecAttn.focus();
	}
	else if (document.PoEntry.txtReasonFor.value =="")
	{
		alert ("Please enter a reason for this request and try again.");
		document.PoEntry.txtReasonFor.focus();
	}
	else
	{
		returnValue=true;
	}
	return returnValue;
}

Hope this helps,
Dan
 
changing to else if seemed to fix the problem. Thanks for all the suggestions, I really appreciate it!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top