...

View Full Version : Cant get the focus on the first element of an error array



SunJune
12-13-2010, 06: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>
=======================================================

Philip M
12-13-2010, 07:46 PM
If multiple errors exist, allow the user to correct them progressively one at a time. Otherwise use a variable named firstError which will take the value 1,2,3 etc., and then focus on element "text" + firstError.

Form validation of the pattern if (text1.value==""||text1.value==null) is barely worthy of the name, and virtually useless, as even a single space, an X or a ? will return false, that is pass the validation. Numeric values, such as zip codes and phone numbers, should be validated as such. Ditto email addresses. This topic has been covered many times before in this forum.

The value of a textbox cannot be null - only the string value "" (blank). Null is a reserved word and cannot be used for anything other than to check that a variable does not have an assigned value. To make this clear:-


<input type = "text" id = "mytext" >

<script type = "text/javascript">
var x = document.getElementById("mytext").value; // x is a string
if (x == null) {
alert ("Field is BLANK"); // no alert shows
}
var y;
if (y == null) {
alert ("y is null")
}
</script>
All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

SunJune
12-13-2010, 08:21 PM
Thanks Philip for the response. I was able to perform what you suggested, i.e. to validate one field at a time, I am curious how I can apply the concept of getting the focus to the first element of an array which stores the errors.

Philip M
12-13-2010, 10:04 PM
Thanks Philip for the response. I was able to perform what you suggested, i.e. to validate one field at a time, I am curious how I can apply the concept of getting the focus to the first element of an array which stores the errors.

I did explain that in Post #2. Use a variable named firstError which will take the value 1,2,3 etc., and then focus on element "text" + firstError.

document.form.element(errors[0]).focus();
There is no such form field.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum