Google Answers Logo
View Question
 
Q: java script - validation routine (easy question) ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: java script - validation routine (easy question)
Category: Computers > Internet
Asked by: teacholddognewtricks-ga
List Price: $4.00
Posted: 12 Sep 2004 23:46 PDT
Expires: 12 Oct 2004 23:46 PDT
Question ID: 400393
I have a simple validation routine.  All code is posted below, as well
as the submit button code.  I can run it fine with the first two
validations, but when I add the validation for the city field, I get
an error and if I click No to stop it opens the page I am calling. 
Can I only validate 2 fields???  If so, how can I validate more.  When
I take all this code off and just set the properties on the fields,
that validation process does not kick in and check fields before
moving on.  Below is my code....

It is as follows:
<script language="JavaScript" type="text/javascript"> 

 function validate(FrontPage_Form1){
 

if (document.FrontPage_Form1.LastName.value.length<2)
{ 
   window.alert('Please enter a full last name.'); 
	return false;
  } 
return true;

if (document.FrontPage_Form1.FirstName.value.length<2)
{ 
   window.alert('Please enter a full First name.'); 
	return false;
  } 
return true;

if (document.FrontPage_Form1.City.value.length<1)
{ 
   window.alert('Please enter a valid city.'); 
	return false;
  } 
return true;

 } 
</script>

The is the form info
<form method="POST" action="https:(my file name here)"
target="_parent" onsubmit="return FrontPage_Form1_Validator(this)"
name="FrontPage_Form1" language=javascript>

the submit button code is this:
<input type="submit" onClick="javascript:return validate(this);
" value="Submit">
Answer  
Subject: Re: java script - validation routine (easy question)
Answered By: palitoy-ga on 13 Sep 2004 01:40 PDT
Rated:5 out of 5 stars
 
Hello teacholddognewtricks

I believe the problem you are having is due to additional "return
true;" lines, you should only "return true;" after the final if
statement check (which means you can add as many more as you like as
long as they do not have any additional "return true" statements after
them).  When I run the script below I have to enter data into all
three fields before I can carry on.

<script language="JavaScript" type="text/javascript"> 
 function validate(FrontPage_Form1){

if (document.FrontPage_Form1.LastName.value.length<2)
{ 
   window.alert('Please enter a full last name.'); 
	return false;
  } 

if (document.FrontPage_Form1.FirstName.value.length<2)
{ 
   window.alert('Please enter a full First name.'); 
	return false;
  } 

if (document.FrontPage_Form1.City.value.length<1)
{ 
   window.alert('Please enter a valid city.'); 
	return false;
  } 
return true;
 } 
</script>

If you need any further help on this please ask for clarification and
I will do my best to respond quickly.
teacholddognewtricks-ga rated this answer:5 out of 5 stars and gave an additional tip of: $4.00
Thank you!!! It worked!! I'm sorry it took so long to respond.  I was
having technical problems and had to wait for the hosting service to
reset frontpage extensions.  Thank you, again!

Comments  
Subject: Re: java script - validation routine (easy question)
From: thekok-ga on 17 Sep 2004 11:06 PDT
 
The return true is first problem .
Second problem I can see is :
You should do the validate(this) on the onsubmit instead of
onclick.
Like this :

The is the form info
<form method="POST" action="https:(my file name here)"
target="_parent" onsubmit="return validate(this);"
name="FrontPage_Form1" language=javascript>

the submit button code is this:
<input type="submit"  value="Submit">

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy