Focus Form

Discussion in 'Javascript' started by DonO, Oct 5, 2009.

  1. DonO

    DonO Guest

    Not sure why this isn't working. I'm building a function to check the
    value of a form field to make sure it's numeric or a comma or period.
    I know this could probably be done with a regex function, but that's
    not my strong suit...

    The part I don't understand is when it throws the error that the field
    doesn't match the criteria, it fails to put the focus on the field. It
    highlights it, but focus goes to the next text box.

    Thanks,
    D.

    <script type="text/javascript">
    function check_numeric(inObj){
    var allowedChars = "1234567890,.";
    var checkStr = inObj;

    var curChar = '';
    var chkChar = '';
    var isNumber = 1;

    for(i=0; i<checkStr.value.length; i++){
    curChar = checkStr.value.charAt(i);
    for (j = 0; j < allowedChars.length; j++){
    if (allowedChars.charAt(j) == curChar) break;
    if (j == (allowedChars.length-1)) isNumber = 0;
    }
    if(isNumber == 0){
    alert('Only the following characters are allowed:\n
    \n'+allowedChars);
    document.getElementById
    (inObj.id).style.background="#f99";
    inObj.focus(); // not working
    break;
    }
    }
    if(isNumber == 1){
    document.getElementById(inObj.id).style.background="#fff";
    }
    }
    </script>
    DonO, Oct 5, 2009
    #1
    1. Advertising

  2. On Oct 5, 10:04 pm, DonO <> wrote:
    > Not sure why this isn't working. I'm building a function to check the
    > value of a form field to make sure it's numeric or a comma or period.
    > I know this could probably be done with a regex function, but that's
    > not my strong suit...


    Something like this one?

    Code:
    function isNumeric(input_id)
    {
    var arr = document.getElementById(input_id).value.split(','),
    curr;
    for (var i = 0, len = arr.length; i < len; i++)
    {
    curr = arr[i];
    if (Number(curr) != curr || /^\s*$/.test(curr))
    {
    return false;
    }
    }
    return arr.length;
    }
    
    See that expression:
    Number(curr) != curr

    Internaly that will be looks:
    Number(curr) != ToNumber(curr)
    11.9.3 The Abstract Equality Comparison Algorithm in ECMA 3

    The real benefit of that technique is NaN. Because comparison any NaN
    value results will be false. See 8.5 The Number Type from ECMA 3

    Regards.
    Asen Bozhilov, Oct 5, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason
    Replies:
    4
    Views:
    1,590
    Jason
    May 7, 2007
  2. Replies:
    2
    Views:
    325
    Thomas 'PointedEars' Lahn
    Jun 6, 2005
  3. Roger
    Replies:
    3
    Views:
    306
  4. Jeff

    manual focus vs. focus()

    Jeff, May 20, 2007, in forum: Javascript
    Replies:
    4
    Views:
    169
  5. Spizzat2
    Replies:
    2
    Views:
    169
    Spizzat2
    Jan 9, 2008
Loading...

Share This Page