I have a java validator that is very simple, if the field contains values not in the validator, it clears the field … problem is, it also moves the cursor focus to the next field.
I need the focus to be in the same field…
also need to make sure the field value default is unchanged.
Can anyone help me with this…
I know NOTHING about JS…
The onBlur or onChange event has same effect.
the code is as follows…
In the header —
var numb = “0123456789,”;
function res(t,v){
var w = “”;
for (i=0; i < t.value.length; i++) {
x = t.value.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}
t.value = w;
}
In the EVENT —
res(this,numb)
it does what is supposed to do in that it will not allow entry of any character not in the var value… but the focus and default value are my big problem…
onEvent code should return a value. true (the JavaScript boolean value) means that the event can proceed, false means that it cannot. Adjust your call to the validation code to read:
return res(param1, param2)
and make sure that res() returns false when validation fails, and true when validation succeeds.
Hi Stan… I added … return res(this,numb) in the event.
Now it cleans up the field as its supposed to, but instead of returning focus to field, focus is gone. I need to reset focus and the default value of 0 to the field in the validation event. If the field does validate correctly, it does move to the next field and focus.
Any chance you can help me with that. If I get this working right it I will post it… a lot of people seem to have this problem. This routine works good and is easy to understand.
The code should probbly go into the onblur event rather than the onchange, since losing focus is what you’re trying to prevent. Setting the default value is just a matter of adding this:
argument[0].value = ;
just before the “return false;” line. Remember, the first argument to the function call is the field object, so you can refer to it using the arguments collection.