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?
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.
Joined: Jul 31, 2001
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.
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.
Joined: Jul 31, 2001
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.
Joined: Oct 12, 2000
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).