Return focus to text input field that caused the error

A

Andrew Falanga

Hi everybody,

I've written a simple function to check that dates in my forms are
entered in the format that I need them to be in. I'm using the
onchange event attribute. However, it seems that to register the
change one must leave the field in question, tab or otherwise, and
then the script is run. I'd like to have it so that if there's an
error, the cursor is placed back in that field rather than the user
having to return. How would I do this?

Thanks,
Andy
 
T

Thomas 'PointedEars' Lahn

Andrew said:
I've written a simple function to check that dates in my forms are
entered in the format that I need them to be in. I'm using the onchange
event attribute. However, it seems that to register the change one must
leave the field in question, tab or otherwise, and then the script is
run.

That is how the `change' event is defined. RTFM.
I'd like to have it so that if there's an error, the cursor is placed
back in that field rather than the user having to return.

No, you don't. What you want is a recipe for disaster, an accessibility
nightmare, and although there are currently means to work around vendor's
precautions against such script-kiddie nonsense, there is no guarantee that
those workarounds will work later.
How would I do this?

You don't. Client-side form validation is supposed to *help* users.


PointedEars
 
T

Thomas Allen

Hi everybody,

I've written a simple function to check that dates in my forms are
entered in the format that I need them to be in.  I'm using the
onchange event attribute.  However, it seems that to register the
change one must leave the field in question, tab or otherwise, and
then the script is run.  I'd like to have it so that if there's an
error, the cursor is placed back in that field rather than the user
having to return.  How would I do this?

In general, you would use the focus method on the desired element to
do this. Make sure to user test, because as PointedEars said, it's
usually an accessibility issue. I don't think that it's fair to
altogether throw out the idea, however, and you (hopefully) know
better than we do what your users want.

Thomas
 
D

Dr J R Stockton

In comp.lang.javascript message <ba9b271f-fd20-4cb7-a8f2-c212843ac1d9@d7
g2000prl.googlegroups.com>, Fri, 17 Apr 2009 13:05:00, Andrew Falanga
I've written a simple function to check that dates in my forms are
entered in the format that I need them to be in. I'm using the
onchange event attribute. However, it seems that to register the
change one must leave the field in question, tab or otherwise, and
then the script is run. I'd like to have it so that if there's an
error, the cursor is placed back in that field rather than the user
having to return. How would I do this?

One should allow the entry of an "illegal" date as part of the process
of correcting the error of typing a legal date or part-date of incorrect
value.

It would be possible to use a timeout to validate on the date not being
changed for a suitable interval; or to insist that the date be followed
by a non-date character (space) to trigger checking. Both would need
user training.

Look at <URL:http://www.merlyn.demon.co.uk/js-valid.htm#VFF>, after
reading the rest of that page, for a compact way of expressing, on an
Object literal, the validation of the individual fields of forms.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top