JavaScript Forum

Alert and Input Control Events

Dec 29, 2011 11:28 pm
Gene Wirchenko

Dear JavaScripters:

Does using alert() cause input control events to fire?

I am writing some validation routines where I want the control to
keep the focus if the test fails. (Yes, I know that this is
desktop-app style. That is what I want.)

Actually, if onblur fires, the focus has already moved. To get
around this, I have a global that is set if validation is already
occurring on another input control. In that case, I simply bypass the
validation on the second control so that the focus can be set back.

This works (IE 9 & Windows 7).

It seems that the order of statements is important. I have to
set the focus back before generating the error alert. Otherwise, it
does not work right, and I can get the other input control's
validation routine getting in the way. I also had the hilarity of my
debugging alert() statements affecting the program flow.

It seems to me that alert() might cause input control events to
fire. Is this so? If so, is it guaranteed to be like this, or is it
just IE 9's implementation? How tied to implementation is this error
handling making my code?

Sincerely,

Gene Wirchenko

Dec 31, 2011 12:27 am
Stefan Weiss
Re: Alert and Input Control Events

On 2011-12-30 00:28, Gene Wirchenko wrote:
> I am writing some validation routines where I want the control to
> keep the focus if the test fails. (Yes, I know that this is
> desktop-app style. That is what I want.)

Maybe. A word of caution, though: don't try too hard to control the
users' focus:

1) you can still deny the submission of an incorrectly filled-out
form later on,
2) you can't control the focus reliably across different platforms,
anyway, and most imporantly
3) there may be a good reason why the user wants the focus somewhere
else

What you describe is pretty much the way the forms in one of my projects
worked (by customer request) up until a few years ago. Then it turned
out that this behavior was perceived as more of a hindrance than a help.
It's usually better to visually mark the field as invalid and let the
users fill out the other fields in the order they want to.

A trivial (and admittedly construed) example where gluing the focus to
one particular field would be harmful is a 3-field date input:

[2011] [11] [30] (pre-filled values)

The user realizes that the correct value should be 2011-12-31, and
starts by changing the "day" field, entering "31". This results in a
temporarily invalid date - but they can't leave the "day" field to
adjust the "month" field. They will swear at the "stupid braindead app",
click away the alert, enter "30", change the month, and finally enter
"31" again.

Now we just let them fill out invalid values as they like, color the
respective field labels red, and wait until they try to submit the form
before we show any alerts or change the focus.

> I also had the hilarity of my debugging alert() statements affecting
> the program flow.

Yup, it can do that :)
Especially when events and/or timeouts are involved. Using console.log()
for debugging gets rid of all that.

> It seems to me that alert() might cause input control events to
> fire. Is this so? If so, is it guaranteed to be like this, or is it
> just IE 9's implementation? How tied to implementation is this error
> handling making my code?

Sorry, I can't help there (my experience with IE is rather limited).
I realize that what I wrote doesn't have much to do with JS per se, and
I didn't answer your original question, but maybe this helps anyway.


- stefan


Jan 1, 2012 7:40 am
Gene Wirchenko
Re: Alert and Input Control Events

On Sat, 31 Dec 2011 01:27:32 +0100, Stefan Weiss
<krewecherl@gmail.com> wrote:

[snip]

>A trivial (and admittedly construed) example where gluing the focus to
>one particular field would be harmful is a 3-field date input:
^^^^^^^^^^^^^^^^^^
Yech!

> [2011] [11] [30] (pre-filled values)
>
>The user realizes that the correct value should be 2011-12-31, and
>starts by changing the "day" field, entering "31". This results in a
>temporarily invalid date - but they can't leave the "day" field to
>adjust the "month" field. They will swear at the "stupid braindead app",
>click away the alert, enter "30", change the month, and finally enter
>"31" again.

I am referring to field-level validation, not form-level
validation. If a field value itself is invalid *without reference to
any other fields*, I will throw an error. In the above example,
entering a month 13 is such an error. Entering a day 32 is such an
error. Entering a day 31 is not such an error.

If the problem is that two or more field values are incompatible
(say, month 2 and day 30), that is a form-level error and I would
handle that on form submit.

The principle is to catch the error as soon as possible.

[snip]

Sincerely,

Gene Wirchenko


Jan 1, 2012 11:02 am
Gregor Kofler
Re: Alert and Input Control Events

Am 2012-01-01 08:40, Gene Wirchenko meinte:
> On Sat, 31 Dec 2011 01:27:32 +0100, Stefan Weiss
> <krewecherl@gmail.com> wrote:

> I am referring to field-level validation, not form-level
> validation. If a field value itself is invalid *without reference to
> any other fields*, I will throw an error. In the above example,
> entering a month 13 is such an error. Entering a day 32 is such an
> error. Entering a day 31 is not such an error.
>
> If the problem is that two or more field values are incompatible
> (say, month 2 and day 30), that is a form-level error and I would
> handle that on form submit.

Do you *really* want to use alert dialogs for such errors (and fiddling
with element focus)? Usability will be painful, to say the least. Even
more so, when there will be a second validation run upon submission
(with probably a different way to inform the user about errors). I'd opt
for blur listeners giving consistent feedbacks, and *not* fiddling with
element focus.

Gregor


http://vxweb.net


Previous Thread: Eval() or What?
Next Thread: JavaScript's Character Escaping Functions

Related Forum Topics
Submitting a Form that has Control Events
Dear JavaScripters:

I am putting what I thought would be the final touches on the
first cut for a form handler.

I have validation on controls and on the form as a whole. The
control-level validations work just fine, and focus does not move off
the control. This is what I...
How to control (disable/enable) all onclick events
Hi,

I need to implement API by which all onclick envets in same document
can be in flexible control (disable/enable on demand).

I tried the followings:

1. overwrite onclick function:
if...
Jquery ,get the id of the input in order to delete input row?
Below is the delete function, but I don't know how to capture the
input id. I am not using an html table for the data. The todo items
are added dynamically through AJAX. How to select which id has the
button delete clicked and remove that row ? The html is in a section
and is an...
Alert box and pop up blockers?
How have you solved the usage of alert box (or other modal message box),
when
users have pop up blockers active?

In the testing alert boxes seem to be a reliable way to interact with
users. But some designers use a div, which covers the
whole page so that the use has to (?) click that...
Alert and pop-up blockers
It seems to me that pop up blockers do not prevent alert, confirm, and
prompt boxes appearing. Or do some prevent?

If there are significant number of (also alert-box-) blockers in use, how to
make an own box (e.g. a div), which does prevent the user proceeding
without clicking the...
How to make a multiline alert?
Here is the beginning of the function:
print "function validateFormData(vtf2) {
var x = vtf2.ccn.value
var msg = \"\";
var newline = \'CR\';
if (x==null || x==\"\") {
vtf2.ccn.bgColor=\"lightred\";
...
Onclick Works With Alert(), But Not My Function
I suspect this is a case of not seeing the obvious, but it's been
bugging
me for a couple of days now.

I have a page generated by a PHP script. I have radio buttons with
onclick
properties set, but I can't tell if my functions work because they
never get
called (as far as I know). Yet...
Is there a map or list of events anywhere around?
I see from a couple of days of googling that many folks have tried to
find a knowledgable or even authoritative (ha!) list of events; the
browsers that support them; the DOM objects they might be associated
with; and how to address these events in different browsers.

There is a good list at...
Using Both click and dblClick Events
Dear JavaScripters:

Suppose that one wishes to have an action take place when an
element is clicked and a different one when it is doubleclicked. How
does the event processing work?

From my checking under IE9, if I doubleclick, the click event
will fire twice before the...