Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

focus() in firefox not working properly



8:25 pm on Dec 6, 2005 (gmt 0)

10+ Year Member

I have this javascript code that checks to see if only numbers (and comma's and period's) are present in an html input field, but focus() isn't working in firefox.

function checkNumOnly(thisField) {
if (/[^0-9,.]/.test(thisField.value)) {
alert("Du skrev:\n" + thisField.value + "\n\nmen kun tall er tillatt.");

when i tab out of the input field (and i have letters or something there to trigger the alert) the cursor doesn't get put back in the input field, it goes to the next thing on the page:

Text2 <input type="text" name="txtfld2" value="" onblur="checkNumOnly(this);" />

Rambo Tribble

1:56 pm on Dec 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member

Firefox, on Linux anyway, has some issues with focus, though I hadn't encountered this before. See [bugzilla.mozilla.org...]

You could try something like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<script type="text/javascript">
<input type="text" onfocus="fldx=this" />
<input type="text" onfocus="fldx.focus();" />


10:41 pm on Dec 7, 2005 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

I tried to get this working but with no luck. My initial thought was that you'd need to add an onkeypress event handler that would stopPropagation of the event if the keyCode was 9 (the tab key) and the input value was invalid. Then you'd need another handler for the onblur event to set the focus back to this one. But I couldn't get it working.

A possible alternative... instead of checking the value on the blur event, you could check it on the form submit event, as part of some validation. Then highlighting and setting the focus should be easier (I think).

Good luck


12:28 am on Dec 8, 2005 (gmt 0)

WebmasterWorld Senior Member kaled is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Try this

globalvar = thisfield;



9:28 am on Dec 8, 2005 (gmt 0)

10+ Year Member

hey cool, i'll give that a try. thanks!

Featured Threads

Hot Threads This Week

Hot Threads This Month