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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird form problem :/

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi,

I'm trying to make a "validatior" for my forms - but for some reason, even where returning 'return false;' it still goes on to the next step :/

The JS code is:

Code:
<script>
	
function Validator(theForm)
{
	if (theForm.name_first_billing.value == "")
	{
		alert("Please enter a \"Billing First Name\".");
		theForm.name_first_billing.focus();
		return false;
	}
	if (theForm.name_last_billing.value == "")
	{
		alert("Please enter a \"Billing Last Name\".");
		theForm.name_last_billing.focus();
		return false;
	}
	if (theForm.address1_billing.value == "")
	{
		alert("Please enter a \"Billing Address\".");
		theForm.address1_billing.focus();
		return false;
	}
	if (theForm.city_billing.value == "")
	{
		alert("Please enter a \"Billing City\".");
		theForm.city_billing.focus();
		return false;
	}
	if (theForm.stateID_billing.value == "0")
	{
		alert("Please select a \"Billing State\".");
		theForm.stateID_billing.focus();
		return false;
	}
	if (theForm.zipcode_billing.value.length < 5)
	{
		alert("Please enter a \"Billing Zipcode\".");
		theForm.zipcode_billing.focus();
		return false;
	}
	if (theForm.phone_billing.value.length < 10)
	{
		alert("Please enter a \"Billing Phone Number\" that includes the area code.");
		theForm.phone_billing.focus();
		return false;
	}
	if (theForm.email.value == "")
	{
		alert("Please enter an \"Email Address\".");
		theForm.email.focus();
		return false;
	}
	if (BadEmail(theForm.email.value))
	{
		alert("The \"Email Address\" is not valid.");
		theForm.email.focus();
		return false;
	}
	if (theForm.name_first.value == "")
	{
		alert("Please enter a \"Ship To First Name\".");
		theForm.name_first.focus();
		return false;
	}
	if (theForm.name_last.value == "")
	{
		alert("Please enter a \"Ship To Last Name\".");
		theForm.name_last.focus();
		return false;
	}
	if (theForm.address1.value == "")
	{
		alert("Please enter a \"Ship To Address\".");
		theForm.address1.focus();
		return false;
	}
	if (theForm.city.value == "")
	{
		alert("Please enter a \"Ship To City\".");
		theForm.city.focus();
		return false;
	}
	if (theForm.stateID.value == "0")
	{
		alert("Please select a \"Shipping State\".");
		theForm.stateID.focus();
		return false;
	}
	if (theForm.zipcode.value.length < 5)
	{
		alert("Please enter a \"Ship To Zipcode\".");
		theForm.zipcode.focus();
		return false;
	}
  
  return (true);
}

</script>

The form is quite long winded.. but consists of:


Code:
  	 <form class="formclass" method="post" id="form1" name="theForm" onsubmit="return Validator(this)" action="order.cgi">

...HTML forms etc

   </form>

I'm really stumped as to why this is happening - so any suggestions are MUCH welcomed!

TIA

Andy
 
In what browser? If it's Fx, then take a look at the JS console (or install Firebug) to make sure no other JS errors are occurring (perhaps due to 'lazy IE' syntax) that might be causing the "returns" to fail.

Does it always fail at the same point?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
As dan said, check in the error console to see if any errors are occurring. All your onblur and onfocus handlers are erroring in firefox. You're using event.srcElement to reference the element that is being blurred or focused. Why not just a this reference instead?

When your validation function is setting focus to an element it's getting an error on the onfocus handler, which is causing your validation function to crash. Any time a validation function crashes, the form will always submit.

Change event.srcElement to this and everything should work.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Dan, you have to keep submitting to the pages till you get to "step 3"

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top