PDA

View Full Version : focus() problem in Netscape and Mozilla


justlearning
09-11-2002, 07:59 PM
I am writing a program that will be used by different people in with different browsers. However, I am running into a problem when I try to force a field to be numeric.

My code is :

function checkNumber(fld) {
var validNum = "0123456789.";
var numOk = "yes";
var tmp;
for (var i=0; i<fld.value.length; i++) {
temp = "" + fld.value.substring(i, i+1);
if (validNum.indexOf(temp) == "-1") numOk = "no";
}
if (numOk == "no") {
alert("This field is numeric. Only numbers and decimals allowed.");
fld.focus();
fld.select();
}

}


The selection is

<input type="text" name="idnum" onBlur="checkNumber(this);">

This code works fine in IE 5.5, Netscape 4.7 and Konquerer 3.0. However, when using Netscape 6.2, Netscape 7.0, Mozilla 1.0 or Mozilla 1.1, after someone clicks on the ok button from the alert, the next field is selected rather than the field that they entered the wrong information in.

The strangest thing is that I also have a validate function from the submit button that checks the this field for length and uses the document.form_name.idnum.focus(); command if it is too small or too long and that focus works correctly.

I have tried hard coding the exact same command to go back to the idnum field and it does not work in the checkNumber function.

Can anyone tell me why my focus is not working in the later browsers and if there is a command or a way to make it work?

Thanx,
JustLearning

Roy Sinclair
09-11-2002, 10:43 PM
Nothing is quite so disturbing as having an alert box pop up when you can get away without one. Look at this simple page for an example of how to do this without alerts for IE, Netscape 4 and Gecko based browsers (Netscape 6/7 and Mozilla):


<html>
<head>
<title>Testing</title>
<script>
function checkForNum(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;
return true;
}
</script>
</head>
<body>
<form name="tstForm">
<input id="tstField" type="text" size="10" onkeypress="return checkForNum(event);"></input>
</form>
</body>
</html>

justlearning
09-11-2002, 11:10 PM
Thank you for your help. I had not thought about using the keycodepress to limit the characters.

I do have one question. How do I allow periods (decimal points) without allowing slashes?

I can expand to include periods by droping the charCode < 48 to charCode < 46, but have not figured out a way to keep the slashes out.


Thanx,
JustLearning

Roy Sinclair
09-12-2002, 10:33 PM
Originally posted by justlearning
Thank you for your help. I had not thought about using the keycodepress to limit the characters.

I do have one question. How do I allow periods (decimal points) without allowing slashes?

I can expand to include periods by droping the charCode < 48 to charCode < 46, but have not figured out a way to keep the slashes out.


Thanx,
JustLearning

The easiest way is to add an alert to the code which displays which keycode you're getting when you press a key. Then test the function and press the key you want to include or exclude, once you've done that you'll know which keycode(s) to add to the inclusions or exclusions and can remove the alert.

justlearning
09-12-2002, 10:49 PM
The trouble is that I already tried that. The period is #46, but when I trouble is that when I try to exclude exclude 47 (the slash) or include 46 without including 47, the whole thing breaks down.

The code is based on anything oustide a range returning false and anything else returning true. I tried to several things, including rewriting it to return true if it fell inside a range and false for everything else, but everything I tried caused the entire script to crash.

Thanx anyway. I guess I will live with them being able to enter a slash. It is a lot better than them clicking okay on the alert and moving forward with the bad characters still there.

justlearning
09-13-2002, 01:06 AM
I figured out what I was doing wrong when trying to modify the script. It is working perfectly for me now.

Thanx for your help.