Problem with focus()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • CharlesTheHawk

    #1

    Problem with focus()

    I thought this would be really simple, but i'm stumped. I'd like to validate some data without submitting the form, but i can't seem to get the focus to go back to the field with the bad data. I've stripped out most of the code just to illustrate the problem. In both IE and Mozilla, the focus still goes to the next element. This is .php generated.

    <html><body><sc ript type="text/javascript">
    var numAdults=5;
    var numChildren=0;
    function Check(element){
    value=parseInt( element.value);
    if(isNaN(value) || value<0 || value>99){
    if(element.name =='txtNumAdults '){
    element.value=n umAdults;
    }else{
    element.value=n umChildren;
    }
    element.focus() ;
    return false;
    }else{
    //SetDirty(elemen t);
    return true;
    }
    }
    </script>
    <form action="a1.php" method="POST" name="myFrm">
    <table>
    <tr><td align="right">< label>Adults:</label></td>
    <td><input type="text" name="txtNumAdu lts" size="3" value="5" onChange="Check (this);" /></td></tr>
    <tr><td align="right">< label>Children: </label></td>
    <td><input type="text" name="txtNumChi ldren" size="3" value="0" onChange="Check (this);" /></td></tr>
    </table></form></body></html>


  • kaeli

    #2
    Re: Problem with focus()

    In article <32e1b9d1ee04f9 fe12d9c262201b5 0a9
    @localhost.talk aboutprogrammin g.com>,
    charlesthehawkn ospam@nospamyah oo.com enlightened us with...[color=blue]
    > I thought this would be really simple, but i'm stumped. I'd like to validate some data without submitting the form, but i can't seem to get the focus to go back to the field with the bad data. I've stripped out most of the code just to illustrate the problem. In both IE and Mozilla, the focus still goes to the next element. This is .php generated.
    >[/color]

    I've seen it before. It has something to do with the onChange. I don't
    remember exactly, but the solution was to change onChange to onBlur,
    since change isn't fired until a text element loses focus anyway. (I
    tried that with your code and it worked.)

    Beware: if a user hits enter to submit the form, the onBlur won't fire.
    Double up the validation so that onSubmit also validates, JIC.


    --
    --
    ~kaeli~
    When two egotists meet, it's an I for an I.



    Comment

    • CharlesTheHawk

      #3
      Re: Problem with focus()

      Kaeli,
      Thanks. Using onBlur() fixes it for IE, but it still doesn't
      work in Mozilla. What's also odd, is that if i add an
      element.select( ); it will select the text, but the focus
      still moves to the next element. Specs require that it works
      in Mozilla also as many of the users have Linux workstations.
      Any suggestions there?

      Comment

      • CharlesTheHawk

        #4
        Re: Problem with focus()

        Heh, i just found out that Mozilla had a new release yesterday
        that is supposed to fix this. We shall see. Always nice to
        know *I* wasn't doing anything wrong.

        Comment

        • kaeli

          #5
          Re: Problem with focus()

          In article
          <85ad9550ef9756 b639426360d1b7c f9a@localhost.t alkaboutprogram ming.com>,
          charlesthehawkn ospam@nospamyah oo.com enlightened us with...[color=blue]
          > Heh, i just found out that Mozilla had a new release yesterday
          > that is supposed to fix this. We shall see. Always nice to
          > know *I* wasn't doing anything wrong.
          >
          >[/color]

          Good to know.
          Unfortunately, my Unix users use NN7 and I haven't tested this in that
          browser.
          I ended up moving all my validation to onSubmit awhile ago when I had to
          support that POS NN4.

          --
          --
          ~kaeli~
          Support your local medical examiner: die strangely!



          Comment

          Working...