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

Forms 1

Status
Not open for further replies.

tnbrwneyez

Programmer
Oct 2, 2004
19
US
I need to take the following script and change the validate function so that after a messsage is displayed indicating that a field is wrong, the cursor is moved to that field.

<html>
<head>
<title>Form Example</title>
<Script language="JavaScript" type="text/javascript">
function validate() {
if (document.form1.yourname.value.length < 1){
alert("Please enter your full name.");
return false;

}

if (document.form1.address.value.length < 3) {

alert("Please enter your address.");
return false;
}

if (document.form1.phone.value.length < 3) {
alert("Please enter your phone number.");
return false;

}

return true;

}

</script>

</head>
<body>
<h1>Form Example</h1>
<p>Enter the following information. When you press the Submit button, the date you entered will
be validated, then sent by email. </p>
<form name="form1" action="mailto:user@host.com" enctype="text/plain"
method"POST" onSubmit="return validate();">

<p><b>Name:</b> <input TYPE="Text" size="20" name="yourname">
</p>
<p><b>Address:</b> <input TYPE="Text" size="30" name="address">
</p>
<p><b>Phone:</b> <input TYPE="Text" size="15" name="phone">
</p>
<p><input TYPE="Submit" Value="Submit"> </p>
</form>
</body>
</html>

Thanks!!!
 
Code:
function validate()  {
if (document.form1.yourname.value.length < 1){
alert("Please enter your full name.");
[red]document.forms['form1'].elements['yourname'].focus();[/red]
return false;

}

if (document.form1.address.value.length < 3)  {

alert("Please enter your address.");
[red]document.forms['form1'].elements['address'].focus();[/red]
return false;
}

if (document.form1.phone.value.length < 3)  {
alert("Please enter your phone number.");
[red]document.forms['form1'].elements['phone'].focus();[/red]
return false;

}

return true;

}

--Chessbot

"See the TURTLE of enormous girth!"
-- Stephen King, The Dark Tower series
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top