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.

smartin35

IS-IT--Management
Nov 13, 2001
29
US
I am newbie to javascript and am trying to figure out a form validation problem. I have created the following function:

<SCRIPT LANGUAGE="javascript">

function checkform()
{
if(document.info.first.value == '')
{
alert('Please fill out the First Name field to continue');
}


if(document.info.last.value == '')
{
alert('Please fill out the Last Name field to continue');
}


if(document.info.address.value == '')
{
alert('Please fill out the Address field to continue');
}


if(document.info.city.value == '')
{
alert('Please fill out the City field to continue');
}


if(document.info.state.value == '--Make a Selection--')
{
alert('Please choose a State from the dropdown box to continue');
}


if(document.info.zip.value == '')
{
alert('Please fill out the 5-digit zip code field to continue');
}

}

</SCRIPT>


The FORM tag is as follows:

<FORM NAME="info" ACTION="order.pl" METHOD="post">

The order.pl prints the information entered on another screen.

I am using the following INPUT type with an onclick variable to call the function:

<INPUT type="submit" onclick="return checkform()" VALUE="Buy Now">

When the function is called it will pop the proper alert but instead of remaining on the page to allow the user to input the proper data it continues on and passes the incomplete data to the order.pl program.

Example: Note the imcomplete Last Name field that was passed.

First Name: Steve
Last Name:
Address: 123 Main
City: Nowhere
State: KS
Zip: 12345

Any suggestions appreciated. Thanks.

Steve
 
Try changing the input type to a button instead of submit and sticking a return false; under each alert box. At the end of the script you could use document.forms[0].submit();
 
yup, as supra said, u need to have a return false;
its better to use this function in the form's onSubmit event:
<form .... onSubmit="return checkform()">

one more thing, sometimes if there is an error in the JS code then it will submit the form...

Known is handfull, Unknown is worldfull
 
I added the return false for each box and it works great. Thanks for your responses.

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top