The CreativityGoblin dropped in on me today, and as a result I've been tackling the challenge of entering dates in to a web application. In the past, I've used DHTML calendar widgets for this purpose (my favourite is Mishoo's highly configurable, standards compliant JS Calendar) but while widgets like this have a great deal of “wow” factor I'm not convinced that they are the best entry mechanism when it comes to raw user speed. Today's experiment was partially inspired by PHP's
strtotime function, which accepts a string in a wide variety of formats and converts it in to a time.
Finally, the code uses an error handling technique I picked up on Ward's Wiki called the SamuraiPrinciple, which states
you should either complete your contract and return a valid result, or throw an exception. This is used by the handlers and the main date parsing function itself, with thrown exceptions only caught by the magicDate function attached directly to the onblur event of the input box.
This is a nice piece of coding by Simon and a nice article with some good code links. Mishoo is the primary developer of htmlarea. Speakling of htmlarea, the latest development version can include a spell checker.