aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes The onChange event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "The onChange event" Watch "The onChange event" New topic
Author

The onChange event

Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
I am working on an application where a script executes as the result of an onChange event for an date input field. This script validates the date and may reformat the date, e.g. 1/1/04 becomes 01/01/2004, and change the value of the input field to the formatted date value. However, I have discovered that if the user goes back and reenters the original, unformatted date field value, i.e. 1/1/04, the onchange event does not fire again when the user exits the date input field and, consequently an invalid, unformatted date may make its way into the application which leads to problems elsewhere. Is there anything that can be done to ensure the onChange event fires again if the user re-enters the original date value?
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
you can try using the onblur event instead which gets fired whenever a field looses focus irrespective of whether the content changed.

This could lead to problematic behaviour though if the user decides to close the browser while the focus is on that field.


42
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Thanks Jeroen,

Unfortunately, that is the case here. I have purposely avoided using onBlur because it leads to other issues. I was just hoping that there was a property or something that could be reset so that the browser would fire another onChange event if the user changed the field value back to the value he/she originally entered, overwriting the value the application sets for the field.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
what other issues does onblur have? I have never run into any issues with onblur, when the browser is closed it does not matter since it is gone!

Use onblur it is the best bet.

Eric
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Although I would prefer otherwise, the application users do not want to see 'hard' errors. Therefore, the event handler is linked to the onChange event so a message is displayed once as a 'warning' and the user assumes responsibility for correcting the field value. If onBlur were used, the user would receive an error every time he/she tabbed thru the field. I would argue that this is a good thing but this is a decision that (however bad) has already been made for me and I am, at least at this point, powerless to change it. Therefore, I am attempting to solve the problem without resorting to using the onBlur event.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
your best bet is to always check the code on the server as well and give back a warning from there (and redisplay the form) if an error occurs.

You should never completely rely on clientside validation only, especially for public applications (for intranet use where you can control the exact client configuration you might be able to get away with it sometimes but even then I'd not rely on it).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The onChange event