"Bartosz Wegrzyn" <> schreef in bericht
news:uFK6b.21077$ y.com...
> I use onblue event to validate fields in my form.
> I do this onblur="return isname()" and so so ...
>
> I have one form with 20 fields.
>
> My problem is that when the focus is for example on the first field of my
> form
> the "name" field and I click somewher or press tab than I loose focus and
my
> isname() function is executed. Everything is fine but the focus was change
> for next field
> lets say "address" and the next function is executed isaddress().
Although
> my isname() function tries to change the focus back to name field the
other
> function is executed.
> This cause infinite loop and I cannot access the form anymore.
>
> I know I can use the onchange() event handler but for me it will not work.
> I need onblur(). Is this code wrong. Maybe the way I code is bad. Please
> help.
>
> THE CODE:
>
>
<code snipped>
>
Hi,
If you insist in using onblur you will have to follow a somewhat different
approach. As you have noticed, setting focus to a field that did not
validate triggers an onblur for a field that should not be validated yet.
You will have to suppress the validation for the second field.
One way of solving this is using only one function for validating, passing
the name of the field to be validated, and do validations for that field
only until there is no need to reset focus.
var nowValidatingField='';
function val(nameOfField){
// return directly if validating another field
if (nowValidatingField!=''&&nowValidatingField!=nameO fField) return;
// set global var to suppress other validations
nowValidatingField=nameOfField;
// do validations for each field
if (nameOfField=='field1'){
if (!OK){
alert ('this is wrong')
document.form1.field1.focus();
document.form1.field1.select();
return false
}
}
//etc..
// so all is ok! reset global var to release function for new field.
nowValidatingField='';
return true
}
Fred
|