• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The onChange event

 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic