JavaScript date validation

I was searching the web for JavaScript date validation code. All I found were lots of really bad ideas. Some were quite complicated. John Gang was on the right track. I didn’t see Chris Hogben’s article initially, but he used the right algorithm.

Still, nobody offered the complete solution. Given a date string YYYY-MM-DD, is the date valid?

The smart way to do this is to (a) parse the date string with a regular expression, (b) construct a date object and (c) compare what you got out of the date object to what you put in. To make this really convenient, we’ll use JavaScript’s proptotype functionality to extend the String class, making this available to any string:

String.prototype.isValidDate = function() {
  var IsoDateRe = new RegExp("^([0-9]{4})-([0-9]{2})-([0-9]{2})$");

  var matches = IsoDateRe.exec(this);
  if (!matches) return false;
  

  var composedDate = new Date(matches[1], (matches[2] – 1), matches[3]);

  return ((composedDate.getMonth() == (matches[2] – 1)) &&
          (composedDate.getDate() == matches[3]) &&
          (composedDate.getFullYear() == matches[1]));

}

Here’s the method in action:

var a = "2007-02-28";
alert(a + (a.isValidDate() ? " is a valid date" : " is not a valid date"));

Come to my SXSW panel on user interface consistency

At this year‘s SXSW Interactive festival, I’ll be moderating the panel Getting to Consistency: Don’t Make Your Users Think. The blurb:

Predictable and consistent software is much easier to use. This session explores interface consistency, examples of consistency failures and their consequences.

This is going to be a really interesting discussion. I have three fantastic panelists:

  • Steve Johnson, Senior Manager, User Experience, Adobe
  • Jennifer Fraser, Lead User Experience Designer, Corel
  • Alex Graveley, User Interface Engineer, VMware

We’re going to explain what consistency really means to you and your users and show what it can do for you. What happens when you’re not consistent? We’ll demonstrate examples of failures from the silly to the catastrophic and list the obvious (and hidden) costs of inconsistency.

Sometimes, however, you don’t want to be consistent. We’ll explain why.

Come to this session and be the new best friend of your performance, security, accessibility, tech support and product documentation teams. (Then, go to the session on managing social networks and information overload.)

Address Book AppleScript example

Here’s a simple AppleScript that exports all of the email addresses in your Mac OS X address book as a comma-separated list:

tell application “Address Book”

set emailList to {}

set peopleCount to (count every person)

repeat with i from 1 to peopleCount

set emailList to emailList & (get value of every email of person i)

end repeat

set outputFileName to choose file name with prompt “Save address book as text:” default name “addressbook.txt”

set outputFile to open for access outputFileName with write permission

repeat with e in emailList

write e & “, ” to outputFile

end repeat

close access outputFile

end tell