Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Cant get the focus on the first element of an error array

    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>
    =======================================================

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,313
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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:-

    Code:
    <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.
    Last edited by Philip M; 12-13-2010 at 08:04 PM. Reason: Typo

  • #3
    New Coder
    Join Date
    Dec 2010
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,313
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by SunJune View Post
    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.
    Last edited by Philip M; 12-13-2010 at 10:06 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •