SunJune
12-13-2010, 05:53 PM
Hi,
Below is the code which is used to validate the entries on a form(some field are not be left blank). The user gets the msg when he hits the "Check"button. The problem is after the user gets the msg, I am not able to set the focus in the field which is the first element of an error array which stores the info about the fields with errors on this form.
Please help.
==========================================================
<html>
<head>
<script type="text/javascript">
function check_alert()
{
var errors=[];
if (text1.value==""||text1.value==null)
{
errors[errors.length]="Please enter the Company Name";
}
if(text3.value==""||text3.value==null)
{
errors[errors.length]="Please enter the Address";
}
if(text5.value==""||text5.value==null)
{
errors[errors.length]="Please enter the City";
}
if(text6.value==""||text6.value==null)
{
errors[errors.length]="Please enter the Zip/Costal Code";
}
if(text7.value==""||text7.value==null)
{
errors[errors.length]="Please enter the Country";
}
if (errors.length>0)
{
reportErrors(errors);
return false;
}
else return true;
}
function reportErrors(errors)
{
var msg="There are some errors..\n";
var numerror;
for (var i=0;i<errors.length;i++)
{
numerror=i+1;
msg+="\n"+numerror+"."+errors[i];
}
alert(msg);
document.form.element(errors[0]).focus();
}
</script>
</head>
<body onload="text1.focus()">
Company:<input type="text" name="text1" /><br>
Division:<input type="text" name="text2" /><br>
Address1:<input type="text" name="text3" /><br>
Address2:<input type="text" name="text4" /><br>
City:<input type="text" name="text5" /><br>
State:<select name="state">
<option value= "Alabama" selected>Alabama</option>
<option value= "Alaska">Alaska</option>
<option value= "Arizona">Arizona</option>
<option value= "Arkansas">Arkansas</option>
</select><br>
Zip/Postal Code:<input type="text" name="text6"/><br>
Country:<input type="text" name="text7" /><br>
<input type="button" value="Check" onclick="return check_alert()"/>
</body>
</html>
=======================================================
Below is the code which is used to validate the entries on a form(some field are not be left blank). The user gets the msg when he hits the "Check"button. The problem is after the user gets the msg, I am not able to set the focus in the field which is the first element of an error array which stores the info about the fields with errors on this form.
Please help.
==========================================================
<html>
<head>
<script type="text/javascript">
function check_alert()
{
var errors=[];
if (text1.value==""||text1.value==null)
{
errors[errors.length]="Please enter the Company Name";
}
if(text3.value==""||text3.value==null)
{
errors[errors.length]="Please enter the Address";
}
if(text5.value==""||text5.value==null)
{
errors[errors.length]="Please enter the City";
}
if(text6.value==""||text6.value==null)
{
errors[errors.length]="Please enter the Zip/Costal Code";
}
if(text7.value==""||text7.value==null)
{
errors[errors.length]="Please enter the Country";
}
if (errors.length>0)
{
reportErrors(errors);
return false;
}
else return true;
}
function reportErrors(errors)
{
var msg="There are some errors..\n";
var numerror;
for (var i=0;i<errors.length;i++)
{
numerror=i+1;
msg+="\n"+numerror+"."+errors[i];
}
alert(msg);
document.form.element(errors[0]).focus();
}
</script>
</head>
<body onload="text1.focus()">
Company:<input type="text" name="text1" /><br>
Division:<input type="text" name="text2" /><br>
Address1:<input type="text" name="text3" /><br>
Address2:<input type="text" name="text4" /><br>
City:<input type="text" name="text5" /><br>
State:<select name="state">
<option value= "Alabama" selected>Alabama</option>
<option value= "Alaska">Alaska</option>
<option value= "Arizona">Arizona</option>
<option value= "Arkansas">Arkansas</option>
</select><br>
Zip/Postal Code:<input type="text" name="text6"/><br>
Country:<input type="text" name="text7" /><br>
<input type="button" value="Check" onclick="return check_alert()"/>
</body>
</html>
=======================================================