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.

// Magstripe reader / card swiper ID data follows AAMVA license spec
var magstripeReader = { Data: "", Start: null, IdStartSentinel: ";", IdEndSentinel: "?", IdFieldSeparator: "=" };

function magstripeIdReaderListen() {
    // Record keystrokes from card reader
    $(document).on("keydown", function magstripeRead (event) {
        event.preventDefault();

        // Begin card swipe
        if (event.key == magstripeReader.IdStartSentinel) {
            magstripeReader.Data = "";
            magstripeReader.Start = new Date();
        }
        // Swipe started, read card data
        else if (magstripeReader.Start) {
            if (event.key == magstripeReader.IdEndSentinel) {
                magstripeReader.Start = null;

                var cardData = magstripeIdReaderParseData();
                console.log(cardData);

                $(document).off("keydown");
                return;
            }
            magstripeReader.Data += event.key;
        }
    });
}

function magstripeIdReaderParseData() {
    // State ISO IIN as defined by AAMVA at start of track 2
    var stateCode = magstripeReader.Data.substr(0, 6);

    var fields = magstripeReader.Data.split(magstripeReader.IdFieldSeparator);
    var id = fields[0].substr(6); // ID# starts after 6 char state ISO IIN
    var dob = fields[1].substr(4) // DOB starts after 4 char expiration (CCYYMMDD format)

    if (fields[2].length > 0) {

        // Michigan uses this overflow field as an encoded letter prefix
        if (stateCode == "636032") {

            var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
            var idprefix = alphabet[fields[2] - 1];

            id = idprefix + id;
        }
        else {
            id += fields[2]; // ID overflow by default
        }

    }

    return { Id: id, StateIIN: stateCode, DOB: dob };
}

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!