Click to See Complete Forum and Search --> : Eternal error loop


James B
12-06-2002, 04:28 AM
Hi,

thanks to Dave Clark, I am now able to check all my textboxes for the input value.
However, when an error state is raised, the focus is set back to the textbox involved.
This works fine on the very first textbox in the form.
When I trigger an error on the second textbox, it displays the correct error message, but at the same time it displays an error on the next textbox. The next textbox got the focus for a brief second, and therefor thinks the input is not valid.

This is the script I use:
-----------------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
function validatehoogte(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Ongeldige invoer ! Alleen cijfers zijn toegestaan");
field.focus();
field.select();
}
var nbr = Number(field.value);
if (isNaN(nbr)
|| nbr < 1800
|| nbr > 4999) {
alert("Ongeldig: waarde ligt tussen 1800 en 4999");
field.focus();
field.select();}
}
</script>
---------------------------------------------------------------------
the lines concerned are
field.focus();
field.select();}

The second textbox expects a value between 0 and 3, the third a value between 200 and 500

Have a look at :
http://www.motorexpress.be/ubb/index.htm

At the startup form, just press 'VERZENDEN' , and put a value lower than 1800 in the first textbox on the second page

Thanks!

James

James B
12-06-2002, 01:00 PM
Dave,

you're absolutely right, but for some reason, the onBlur event is the only one that enables this:
field.focus();
field.select();

For some reason, that does'nt work when the function is called using the onChange event??????
Why? How can I work around this to get the same functionality: forcing the cursor back to the field in question.

James

James B
12-06-2002, 04:29 PM
I figured it out:

using the onChange, you get the same effect as with onBlur when you do this:

field.focus();
field.blur();
field.select();

I'll never forget this one ever again!!!

James