469,327 Members | 1,226 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

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><script 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=numAdults;
}else{
element.value=numChildren;
}
element.focus();
return false;
}else{
//SetDirty(element);
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="txtNumAdults" size="3" value="5" onChange="Check(this);" /></td></tr>
<tr><td align="right"><label>Children:</label></td>
<td><input type="text" name="txtNumChildren" size="3" value="0" onChange="Check(this);" /></td></tr>
</table></form></body></html>
Jul 20 '05 #1
4 1628
In article <32e1b9d1ee04f9fe12d9c262201b50a9
@localhost.talkaboutprogramming.com>,
ch******************@nospamyahoo.com enlightened us with...
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.


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.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #2
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?

Jul 20 '05 #3
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.

Jul 20 '05 #4
In article
<85******************************@localhost.talkab outprogramming.com>,
ch******************@nospamyahoo.com enlightened us with...
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.


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!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Dave | last post: by
4 posts views Thread by Nitin | last post: by
1 post views Thread by Ricardo Garcia | last post: by
6 posts views Thread by nizar.jouini | last post: by
1 post views Thread by avnrao | last post: by
1 post views Thread by clickon | last post: by
3 posts views Thread by ngkuhlmann | last post: by
3 posts views Thread by jan.loucka | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.