Using JavaScript to read a drivers license
Reading the magnetic stripe on a US driver’s license card is easy. While every state has different data contained on the license, they often follow the AAMVA card design standards from the American Association of Motor Vehicle Administrators. Check out Annex F in their PDF specification for optional magnetic stripe.
Required hardware
You’ll need a cheap USB magnetic card swipe reader used for credit cards or identification cards. They can be found online for less than $20. The card reader acts a USB keyboard, rapidly inputting the data as keystrokes. Try swiping a card into a text editor- you’ll get the all of the raw data scrunched into the magstripe!
Example for Michigan
For this example, we are attempting to read the state code (ISO), date of birth (DOB), and license number (ID). These are all located on track 2 of the magstripe. The ISO IIN code for Michigan code is 636032. Find the code for your state on the AAMVA ISO IIN page.
Parsing the data
Some states such as Michigan, where I’m located, only use track 2 and do not encode any name or address information on the magstripe. When you swipe the card, the raw data looks something like this fake data:
;636032626035300300=210519650501=11=?
Whoa! Let’s break down the raw data piece by piece.
; | 636032 | 626035300300 | = | 2105 | 19650501 | = | 11 | = | ? |
---|---|---|---|---|---|---|---|---|---|
start sentinel | state ISO IIN | license number | separator | expiration | birthdate | separator | overflow | separator | end sentinel |
6 char | variable length, up to 13 char | 4 char: YYMM | 8 char: CCYYMMDD | variable length, coded prefix letter |
When parsing the data for Michigan, the variable length overflow field is used to hold an alpha character prefixed to the license number (A=1, Z=26). You’ll want to consult the AAMVA card design standards to see how the data can be laid out on the mapstripe for your state!