In this code why is it that when I press the SUBMIT button the focus
only goes back to the Numeric field. What do I need to do to correct
this problem?
Sue
<html>
<head>
<title>JavaScri pt Project</title>
<SCRIPT LANGUAGE="JAVAS CRIPT">
<!--Hide from old browsers
function Validate() {
var themessage = "You are required to complete the following fields:
";
// validate the Firstname
if (document.Regis ter.FirstName.v alue=="") {
alert("Please enter your Firstname");
document.Regist er.FirstName.va lue="";
document.Regist er.FirstName.fo cus();
}
else {
// validate the Lastname
if (document.Regis ter.LastName.va lue=="") {
alert("Please enter your Lastname");
document.Regist er.LastName.val ue="";
document.Regist er.LastName.foc us();
}
else {
// validate Age as be numeric
var YearsOld=docume nt.Register.Age .value;
var YearsOld=parseI nt(YearsOld,10) ;
if (isNaN(YearsOld )) {
alert("Age is not numeric");
document.Regist er.Age.value="" ;
document.Regist er.Age.focus();
}
else {
// validate the @ sign and the period as being the fourth
from the last character in an e-mail address
var RegeMail=docume nt.Register.ema il.value;
var atSign = RegeMail.indexO f("@");
var Period=document .Register.email .value;
var PPeriod = Period.indexOf( '.');
var LPeriod = Period.length - 4;
if (RegeMail == "" || atSign == -1 || LPeriod !=
PPeriod) {
alert("Please enter a valid e-mail address");
document.Regist er.email.value = "";
document.Regist er.email.focus( );
}
else{
// validate the Gender in a drop down menu
var sex=document.fo rms[0].Gender.selecte dIndex;
if (sex==0) {
alert("You must select your GENDER from the drop-down
Menu.");
document.forms[0].Gender.focus() ;
}
else
{
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields:
") {
document.Regist er.submit();
}
else {
alert(themessag e);
return false;
}
}
}
}
}
}
}
//-->
</script>
</head>
<body>
<center>
<form name=Register method="post" action="">
<table border="0" width="90%">
<!-- Begining of the first line of the form for entering data. -->
<tr>
<td>Enter Your Firstname :</td><td align="center">
<Input Type="text" Name="FirstName " value=" "></td>
<td> Ente r Your Age :</td> <td align="center">
<Input Type="numeric" Name="Age" value=" "></td>
<td align="center"> Select your : <SELECT NAME="Gender" SIZE=1 >
<OPTION SELECTED VALUE=""> --- Select Gender ---
<OPTION VALUE="Male">Ma le
<OPTION VALUE="Female"> Female
</SELECT>
</td>
</tr>
<!-- ending of the first line of the form for entering data. -->
<tr>
<td></td>
</tr>
<!-- Begining of the second line of the form for entering data. -->
<tr>
<td align="center"> Enter Your Lastname :</td><td align="center">
<Input Type="text" Name="LastName" value=" "></td>
<td align="center"> Enter Your Email Address :</td> <td
align="center">
<Input Type="text" Name="email" value=" "></td>
<td align="right">< input type=button value="Submit Request"
onclick="Valida te();">
<Input Type="Reset"></td>
</tr>
<!-- ending of the second line of the form for entering data. -->
</body>
</html>
only goes back to the Numeric field. What do I need to do to correct
this problem?
Sue
<html>
<head>
<title>JavaScri pt Project</title>
<SCRIPT LANGUAGE="JAVAS CRIPT">
<!--Hide from old browsers
function Validate() {
var themessage = "You are required to complete the following fields:
";
// validate the Firstname
if (document.Regis ter.FirstName.v alue=="") {
alert("Please enter your Firstname");
document.Regist er.FirstName.va lue="";
document.Regist er.FirstName.fo cus();
}
else {
// validate the Lastname
if (document.Regis ter.LastName.va lue=="") {
alert("Please enter your Lastname");
document.Regist er.LastName.val ue="";
document.Regist er.LastName.foc us();
}
else {
// validate Age as be numeric
var YearsOld=docume nt.Register.Age .value;
var YearsOld=parseI nt(YearsOld,10) ;
if (isNaN(YearsOld )) {
alert("Age is not numeric");
document.Regist er.Age.value="" ;
document.Regist er.Age.focus();
}
else {
// validate the @ sign and the period as being the fourth
from the last character in an e-mail address
var RegeMail=docume nt.Register.ema il.value;
var atSign = RegeMail.indexO f("@");
var Period=document .Register.email .value;
var PPeriod = Period.indexOf( '.');
var LPeriod = Period.length - 4;
if (RegeMail == "" || atSign == -1 || LPeriod !=
PPeriod) {
alert("Please enter a valid e-mail address");
document.Regist er.email.value = "";
document.Regist er.email.focus( );
}
else{
// validate the Gender in a drop down menu
var sex=document.fo rms[0].Gender.selecte dIndex;
if (sex==0) {
alert("You must select your GENDER from the drop-down
Menu.");
document.forms[0].Gender.focus() ;
}
else
{
//alert if fields are empty and cancel form submit
if (themessage == "You are required to complete the following fields:
") {
document.Regist er.submit();
}
else {
alert(themessag e);
return false;
}
}
}
}
}
}
}
//-->
</script>
</head>
<body>
<center>
<form name=Register method="post" action="">
<table border="0" width="90%">
<!-- Begining of the first line of the form for entering data. -->
<tr>
<td>Enter Your Firstname :</td><td align="center">
<Input Type="text" Name="FirstName " value=" "></td>
<td> Ente r Your Age :</td> <td align="center">
<Input Type="numeric" Name="Age" value=" "></td>
<td align="center"> Select your : <SELECT NAME="Gender" SIZE=1 >
<OPTION SELECTED VALUE=""> --- Select Gender ---
<OPTION VALUE="Male">Ma le
<OPTION VALUE="Female"> Female
</SELECT>
</td>
</tr>
<!-- ending of the first line of the form for entering data. -->
<tr>
<td></td>
</tr>
<!-- Begining of the second line of the form for entering data. -->
<tr>
<td align="center"> Enter Your Lastname :</td><td align="center">
<Input Type="text" Name="LastName" value=" "></td>
<td align="center"> Enter Your Email Address :</td> <td
align="center">
<Input Type="text" Name="email" value=" "></td>
<td align="right">< input type=button value="Submit Request"
onclick="Valida te();">
<Input Type="Reset"></td>
</tr>
<!-- ending of the second line of the form for entering data. -->
</body>
</html>
Comment