Javascript and Form Processing

B

billa

I wish to carry out standard form validation (i.e. is it a date?, is
there a value in the field, is it a number) using the onBlur event
rather than the onSubmit event. This (of course) leads to problems when
cancelling an edit as leaving a field to press the cancel button will
trigger the onBlur event and therefore the field validation. Is there
any way to know the destination field / button that caused the onBlur
event? In Ingres ABF/W4GL (mid eighties language of similar
functionality to javascript) there was an available attribute called
the targetfield so the code could run...

onBlur myfield =
{
if (targetfield != CancelButton) {
carry out validation
}
}

Is there something similar in javascript or will I have to program
round the houses i.e. onBlur records which validation to run and
onFocus runs the validation (unless it's a cancel button)?
TIA Bill
 
L

Lee

billa said:
I wish to carry out standard form validation (i.e. is it a date?, is
there a value in the field, is it a number) using the onBlur event
rather than the onSubmit event. This (of course) leads to problems when
cancelling an edit as leaving a field to press the cancel button will
trigger the onBlur event

Don't use onBlur for validation. Use onChange.
There are far too many things that can cause a field to lose focus
before the user is finished entering data.
Using onChange would also allow the onClick handler of your cancel
button to disable your validation.
 
R

RobG

billa said:
I wish to carry out standard form validation (i.e. is it a date?, is
there a value in the field, is it a number) using the onBlur event
rather than the onSubmit event.

You should not rely on onblur, its behaviour differs markedly across
browsers. In some browsers, the onclick is processed before the
onblur, in others it is after. In other browsers, clicking on a
button will not fire the onblur at all - the text input doesn't loose
focus.

If the field doesn't get focus, your validation doesn't run. To fix
your problem, make the onblur validation write an error message to the
page near the input field. Don't be tempted to trap the user in the
field by constantly returning focus to it until it passes validation -
that only annoys those who want to fill in other parts of the form and
return to the field to fix it later.

Run the validation onsubmit as well (just in case the date field was
never given focus).

Now if the onblur validation presents an error message at any time,
the flow of the page is not interrupted and the user has control of
when the error is fixed.
This (of course) leads to problems when
cancelling an edit as leaving a field to press the cancel button will
trigger the onBlur event and therefore the field validation. Is there

That does not happen in all browsers, and because of the different way
events are handled in different browsers (and probably different
versions of the same browser), you can't reliably determine what
caused the onblur to fire.

[...]
 
M

Michael Winter

I wish to carry out standard form validation [...] using the onBlur
event [...]

The blur event is often not a good idea. It will fire /every/ time the
user tries to leave a control and, if you're showing error messages for
failed validation, this can seriously annoy users. Use the change event
/and/ the submit event.

[snip]
Is there any way to know the destination field / button that caused
the onBlur event?

No, there isn't. Sorry.

[snip]

Mike
 
M

Marty

Michael Winter said:
I wish to carry out standard form validation [...] using the onBlur
event [...]

The blur event is often not a good idea. It will fire /every/ time the
user tries to leave a control and, if you're showing error messages for
failed validation, this can seriously annoy users. Use the change event
/and/ the submit event.

[snip]
Is there any way to know the destination field / button that caused
the onBlur event?

No, there isn't. Sorry.

[snip]

Mike

Could you use a hidden form field and change its value (e.g.
document.FormName.HiddenFieldname.value = 'true') with the onBlur event?

Marty
 
L

Lee

Marty said:
Michael Winter said:
I wish to carry out standard form validation [...] using the onBlur
event [...]

The blur event is often not a good idea. It will fire /every/ time the
user tries to leave a control and, if you're showing error messages for
failed validation, this can seriously annoy users. Use the change event
/and/ the submit event.

[snip]
Is there any way to know the destination field / button that caused
the onBlur event?

No, there isn't. Sorry.

[snip]

Mike

Could you use a hidden form field and change its value (e.g.
document.FormName.HiddenFieldname.value = 'true') with the onBlur event?

I don't understand what you're suggesting?
Why a hidden form field instead of a Javascript variable, and
more importantly, how is that going to tell him which control
has focus?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,049
Members
48,769
Latest member
Clifft

Latest Threads

Top