lzhang
06-29-2006, 03:50 PM
I created a small form here with some javascript to validate some fields.
1. If the required field is empty or invalid, you get a prompt and cursor stays within the field.
2. Tab around the phone field if one is filled up.
But now it just clears up all fields if you submit the form. and the tabbing is not working either. Anybody can copy and paste my code and tell my why?
Thank you.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 2</title>
</head>
<body background="images/background.gif">
<form onsubmit="return ValidateMyForm();">
<script>
function ValidateMyForm()
{
var fName = document.MyForm.FName;
var lName = document.MyForm.LName;
var phone = document.MyForm.first + document.MyForm.second+document.MyForm.third ;
var email = document.MyForm.email;
if (fName.value == "")
{
window.alert("Please enter your first name.");
document.MyForm.FName.focus();
return false;
}
if (lName.value == "")
{
window.alert("Please enter a last name.");
document.MyForm.LName.focus();
return false;
}
if (email.value == "")
{
window.alert("Please enter your email address.");
document.MyForm.email.focus();
return false;
}
if (email.value.indexOf("@", 0) < 0)
{
window.alert("Please enter a valid e-mail address.");
document.MyForm.email.focus();
return false;
}
if (email.value.indexOf(".", 0) < 0)
{
window.alert("Please enter a valid e-mail address.");
document.MyForm.email.focus();
return false;
}
if (phone.value == "")
{
window.alert("Please enter your telephone number.");
document.MyForm.first.focus();
return false;
}
return true;
}
</script>
<table border="0" width="93%" id="table1" cellpadding="0">
<tr>
<td colspan="4">
<b><font size="4" face="Verdana" color="#808000">User Registration</font></b><font size="4"> </font></font> <font size="1">
</font>
<b><font face="Verdana" size="1" color="#808000"> </font><font face="Verdana" size="1" color="#FF0000">* </font>
<font face="Verdana" size="1" color="#000080">Required</font></b></td>
<td width="1%">
</td>
<td width="1%"> </td>
</tr>
<tr>
<td width="16%">
<p align="right"><font color="#FF0000" size="1">*</font><font face="Verdana" size="1">First
name:</font></td>
<td>
<font size="1" face="Verdana">
<!--webbot bot="Validation" B-Value-Required="TRUE" I-Minimum-Length="12" I-Maximum-Length="15" -->
<input name="FName" size="15" style="float: left" maxlength="15"></font></td>
<td width="17%">
<p align="right"><font face="Verdana" size="1"> </font><font color="#FF0000" size="1">*</font><font face="Verdana" size="1">Last
name:</font></td>
<td width="43%">
<font size="1" face="Verdana">
<input name="LName" size="12" style="float: left"></font></td>
<td width="1%">
</td>
<td width="1%"> </td>
</tr>
<tr>
<td width="16%">
<p align="right"><font face="Verdana" size="1">Phone:
</font>
</td>
<td width="80%" colspan="3">
<font size="1" face="Verdana">
<script>
function autotab(original,destination){
if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
destination.focus()
}
</script>
<input type="text" name="first" size=1 onKeyup="autotab(this, document.MyForm.second)" maxlength=3></font><font face="Verdana" size="2">
</font><font size="1" face="Verdana">
<input type="text" name="second" size=1 onKeyup="autotab(this, document.MyForm.third)" maxlength=3><font face="Verdana" size="2">
</font>
<input type="text" name="third" size=2 onKeyup="autotab(this, document.MyForm.Fourth)" maxlength=4><font face="Verdana" size="2"> </font></font></td>
<td width="1%">
</td>
<td width="1%"> </td>
</tr>
<tr>
<td width="16%">
<p align="right"><font face="Verdana" size="1"><font color="#FF0000">*</font>Email:</font></td>
<td width="80%" colspan="3"><font size="1" face="Verdana">
<input type="text" name="email" size="29"></font></td>
<td width="1%"> </td>
<td width="1%"> </td>
</tr>
</table> <input type="submit" value="Register Me" name="Register"><input type="reset" value="Clear">
<input type="hidden" name="myform" value="User_Add">
</form>
</body>
</html>
1. If the required field is empty or invalid, you get a prompt and cursor stays within the field.
2. Tab around the phone field if one is filled up.
But now it just clears up all fields if you submit the form. and the tabbing is not working either. Anybody can copy and paste my code and tell my why?
Thank you.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 2</title>
</head>
<body background="images/background.gif">
<form onsubmit="return ValidateMyForm();">
<script>
function ValidateMyForm()
{
var fName = document.MyForm.FName;
var lName = document.MyForm.LName;
var phone = document.MyForm.first + document.MyForm.second+document.MyForm.third ;
var email = document.MyForm.email;
if (fName.value == "")
{
window.alert("Please enter your first name.");
document.MyForm.FName.focus();
return false;
}
if (lName.value == "")
{
window.alert("Please enter a last name.");
document.MyForm.LName.focus();
return false;
}
if (email.value == "")
{
window.alert("Please enter your email address.");
document.MyForm.email.focus();
return false;
}
if (email.value.indexOf("@", 0) < 0)
{
window.alert("Please enter a valid e-mail address.");
document.MyForm.email.focus();
return false;
}
if (email.value.indexOf(".", 0) < 0)
{
window.alert("Please enter a valid e-mail address.");
document.MyForm.email.focus();
return false;
}
if (phone.value == "")
{
window.alert("Please enter your telephone number.");
document.MyForm.first.focus();
return false;
}
return true;
}
</script>
<table border="0" width="93%" id="table1" cellpadding="0">
<tr>
<td colspan="4">
<b><font size="4" face="Verdana" color="#808000">User Registration</font></b><font size="4"> </font></font> <font size="1">
</font>
<b><font face="Verdana" size="1" color="#808000"> </font><font face="Verdana" size="1" color="#FF0000">* </font>
<font face="Verdana" size="1" color="#000080">Required</font></b></td>
<td width="1%">
</td>
<td width="1%"> </td>
</tr>
<tr>
<td width="16%">
<p align="right"><font color="#FF0000" size="1">*</font><font face="Verdana" size="1">First
name:</font></td>
<td>
<font size="1" face="Verdana">
<!--webbot bot="Validation" B-Value-Required="TRUE" I-Minimum-Length="12" I-Maximum-Length="15" -->
<input name="FName" size="15" style="float: left" maxlength="15"></font></td>
<td width="17%">
<p align="right"><font face="Verdana" size="1"> </font><font color="#FF0000" size="1">*</font><font face="Verdana" size="1">Last
name:</font></td>
<td width="43%">
<font size="1" face="Verdana">
<input name="LName" size="12" style="float: left"></font></td>
<td width="1%">
</td>
<td width="1%"> </td>
</tr>
<tr>
<td width="16%">
<p align="right"><font face="Verdana" size="1">Phone:
</font>
</td>
<td width="80%" colspan="3">
<font size="1" face="Verdana">
<script>
function autotab(original,destination){
if (original.getAttribute&&original.value.length==original.getAttribute("maxlength"))
destination.focus()
}
</script>
<input type="text" name="first" size=1 onKeyup="autotab(this, document.MyForm.second)" maxlength=3></font><font face="Verdana" size="2">
</font><font size="1" face="Verdana">
<input type="text" name="second" size=1 onKeyup="autotab(this, document.MyForm.third)" maxlength=3><font face="Verdana" size="2">
</font>
<input type="text" name="third" size=2 onKeyup="autotab(this, document.MyForm.Fourth)" maxlength=4><font face="Verdana" size="2"> </font></font></td>
<td width="1%">
</td>
<td width="1%"> </td>
</tr>
<tr>
<td width="16%">
<p align="right"><font face="Verdana" size="1"><font color="#FF0000">*</font>Email:</font></td>
<td width="80%" colspan="3"><font size="1" face="Verdana">
<input type="text" name="email" size="29"></font></td>
<td width="1%"> </td>
<td width="1%"> </td>
</tr>
</table> <input type="submit" value="Register Me" name="Register"><input type="reset" value="Clear">
<input type="hidden" name="myform" value="User_Add">
</form>
</body>
</html>