Help with focus() after validation

Discussion in 'Javascript' started by Dan, Oct 28, 2003.

  1. Dan

    Dan Guest

    This function validates a price field on my form. It fires with onchange.
    If the validation fails, I want the focus to return to the same field so
    that the user can correct it. The way it is working now, the focus moves
    to whichever field is clicked upon exiting this field. The behavior is the
    same in ie,ns and opera. This is probably simple, but I don't understand
    why this doesn't work Any ideas would be much appreciated. Thanks.

    Dan


    function confirm_customer_unit_price(fieldname,fieldlabel,fieldindex)
    {
    // fieldname: the name of the element
    // fieldlabel: text to be displayed in alert box
    // fieldindex: row number of the element

    // get the value in the field
    var thevalue=document.form1[fieldname].value
    // do the actual validation in another function. this works fine
    var valid=isSignedFloat(thevalue)

    if (valid)
    // if the value is ok, write to an array where I do some more processing
    later
    {customerprice[fieldindex]=thevalue
    return true;}
    else
    {
    // value is not ok. show the alert.
    alert(fieldlabel +' is not valid');
    // write a zero to the array, because I cant use the value that was
    submitted
    customerprice[fieldindex]=0
    // return the focust to the offending field
    document.form1[fieldname].focus();
    //I also tried this, but it doesn't work either
    document.getElementById(fieldname).focus();
    return true
    }
    }
    Dan, Oct 28, 2003
    #1
    1. Advertising

  2. Dan

    kaeli Guest

    In article <z1wnb.49347$Fm2.25196@attbi_s04>,
    enlightened us with...
    > // return the focust to the offending field
    > document.form1[fieldname].focus();


    document.form1.fieldname.focus();

    -------------------------------------------------
    ~kaeli~
    Jesus saves, Allah protects, and Cthulhu
    thinks you'd make a nice sandwich.
    http://www.ipwebdesign.net/wildAtHeart
    http://www.ipwebdesign.net/kaelisSpace
    -------------------------------------------------
    kaeli, Oct 28, 2003
    #2
    1. Advertising

  3. Dan

    Dan Guest

    This function validates a price field on my form. It fires with onchange.
    If the validation fails, I want the focus to return to the same field so
    that the user can correct it. The way it is working now, the focus moves
    to whichever field is clicked upon exiting this field. The behavior is the
    same in ie,ns and opera. This is probably simple, but I don't understand
    why this doesn't work Any ideas would be much appreciated. Thanks.

    Dan


    function confirm_customer_unit_price(fieldname,fieldlabel,fieldindex)
    {
    // fieldname: the name of the element
    // fieldlabel: text to be displayed in alert box
    // fieldindex: row number of the element

    // get the value in the field
    var thevalue=document.form1[fieldname].value
    // do the actual validation in another function. this works fine
    var valid=isSignedFloat(thevalue)

    if (valid)
    // if the value is ok, write to an array where I do some more processing
    later
    {customerprice[fieldindex]=thevalue
    return true;}
    else
    {
    // value is not ok. show the alert.
    alert(fieldlabel +' is not valid');
    // write a zero to the array, because I cant use the value that was
    submitted
    customerprice[fieldindex]=0
    // return the focust to the offending field
    document.form1[fieldname].focus();
    //I also tried this, but it doesn't work either
    document.getElementById(fieldname).focus();
    return true
    }
    }



    "kaeli" <> wrote in message
    news:...
    > In article <z1wnb.49347$Fm2.25196@attbi_s04>,
    > enlightened us with...
    > > // return the focust to the offending field
    > > document.form1[fieldname].focus();

    >
    > document.form1.fieldname.focus();
    >
    > -------------------------------------------------
    > ~kaeli~
    > Jesus saves, Allah protects, and Cthulhu
    > thinks you'd make a nice sandwich.
    > http://www.ipwebdesign.net/wildAtHeart
    > http://www.ipwebdesign.net/kaelisSpace
    > -------------------------------------------------



    in this function, fieldname is a variable. many different text fields use
    this for validation. without the [] notation the script thinks I have a
    field named 'fieldname' and I get the an error something like "fieldname is
    null or not an object.
    Dan, Oct 28, 2003
    #3
  4. Dan

    kaeli Guest

    And on the day Tue, 28 Oct 2003 17:00:30 GMT,
    enlightened us with <Ogxnb.50056$Tr4.106035
    @attbi_s03>...
    > This function validates a price field on my form. It fires with onchange.
    > If the validation fails, I want the focus to return to the same field so
    > that the user can correct it. The way it is working now, the focus moves
    > to whichever field is clicked upon exiting this field. The behavior is the
    > same in ie,ns and opera. This is probably simple, but I don't understand
    > why this doesn't work Any ideas would be much appreciated. Thanks.
    >


    Yeah, and I showed you your error.

    Change
    > > > document.form1[fieldname].focus();

    > >


    to
    document.form1.fieldname.focus();

    --------------------------------------------------
    ~kaeli~
    Kill one man and you are a murderer. Kill millions
    and you are a conqueror. Kill everyone and you
    are God.
    http://www.ipwebdesign.net/wildAtHeart/
    http://www.ipwebdesign.net/kaelisSpace/
    ------------------------------------------------
    kaeli, Oct 28, 2003
    #4
    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. Vinod I
    Replies:
    2
    Views:
    411
    Peter Blum
    Feb 4, 2004
  2. bienwell
    Replies:
    0
    Views:
    372
    bienwell
    Nov 17, 2005
  3. Jason
    Replies:
    4
    Views:
    1,616
    Jason
    May 7, 2007
  4. Mad Scientist Jr
    Replies:
    0
    Views:
    375
    Mad Scientist Jr
    Jul 13, 2004
  5. Roger
    Replies:
    3
    Views:
    320
Loading...

Share This Page