By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,480 Members | 1,766 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,480 IT Pros & Developers. It's quick & easy.

Javascript and Form Processing

P: n/a
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

Oct 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
billa wrote:
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.

[...]

--
Rob
Oct 12 '05 #2

P: n/a
On 12/10/2005 13:38, billa wrote:
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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Oct 12 '05 #3

P: n/a
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.

Oct 12 '05 #4

P: n/a
In article <RE*******************@text.news.blueyonder.co.uk> , Michael Winter
says...

On 12/10/2005 13:38, billa wrote:
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
--
Basic Newsguy 3 GB / month $39.95 / year
http://newsguy.com/overview.htm

Oct 12 '05 #5

P: n/a
Lee
Marty said:

In article <RE*******************@text.news.blueyonder.co.uk> , Michael Winter
says...

On 12/10/2005 13:38, billa wrote:
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?

Oct 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.