Amazing Little Things

One thing I love about programming is that, even after 15 years, I still come across little things that amaze me.

This JavaScript (derived from an answer on stack overflow) toggles the value used by an HTML checkbox.

isChecked ^= 1;

This trivial logic combined with JavaScript’s peculiar type conversion mechanics results in one of the most elegant expressions of intent I’ve ever seen in a single line of code.

Regular Expression for Validating a Social Security Number (SSN) issued after June 25ᵗʰ, 2011

As of June 25ᵗʰ, 2011 the Social Security Administration has changed how SSNs are assigned, and now the regular expressions I’ve traditionally used to validate them are no longer useful. The super-simplistic “is it 9 digits” kind of validation will still work fine, but I’ve always preferred expressions that enforce more of the old SSN structure rules (which were somewhat complex).

Unfortunately, I couldn’t easily locate a good expression online for the new format. The ones I kept finding were either too-simplistic, or just plain flawed (which is often the case with RegEx patterns you find online).

The official article on SSN Randomization from the Social Security Administration describes a structure that will be much simpler than the old one. It seems that the only remaining restrictions are that the areas 666, 000, and 900-999 remain off-limits.

If you dig deeper into the official FAQ there is also the rule that no part (area, group, or serial) will contain all zeros. I couldn’t find any other rules though, so a lot of previously restricted numbers will now be available for assignment.

I also wanted my expression to allow, optionally, for the use of a single dash or space separator character between each group (allowing for mix-n-match separators, which you do see in some systems).

With that set of rules, I come up with the following regular expression for validation.

^(?!000)(?!666)(?!9)d{3}[- ]?(?!00)d{2}[- ]?(?!0000)d{4}$

I ran it against 200k real SSNs, and it validated them all correctly. This pattern should also remain viable for a while, at least until the SSA decides to start giving out the 900-999 area.