Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate Drivers License numbers?

I am working on a ACH payment processor and was wondering if I can scope the drivers license field against some rules. Any ideas on this? Can I assume numbers only or anything else more like SSN

Thanks

like image 977
Quotient Avatar asked Jul 26 '10 19:07

Quotient


People also ask

What are the numbers on Ohio driver's license?

Ohio driver's license numbers are two letters and six numbers. For example, TL545796. Oklahoma driver's license numbers consist of one letter and nine numbers. For example, B000062835 .

What do the numbers on an Illinois driver's license mean?

The first letter in your drivers license number will represent the first letter of your last name. The three numbers following that letter are used as a hashing system to roughly sound like your last name. In the second section, FFF. These numbers will represent your first and middle name.

How many numbers are in an Arizona driver's license?

Arizona driver's licenses and ID cards contain document numbers (circled) that are nine characters- one letter followed by eight numbers.

How many digits are on an Oregon driver's license?

Oregon driver's license numbers are seven numbers. For example, 6110033.


2 Answers

I came up with this for a project I was working on...

function utilities() {
function validateLicense(stateCode, licenseNumber) {
    // from http://www.uiia.org/documents/license_guidelines_08.pdf
    var oneToSevenNumeric = /^[0-9]{1,7}$/;
    var oneAlpha = /(.*[A-Za-z]){1}/;
    var oneAlphaPlusSeven = /^.[0-9]{7}$/;
    var twoAlpha = /(.*[A-Za-z]){2}/;
    var alphaPlusSixNumeric = /(.*[0-9]){6}$/;
    var threeToFiveNumeric = /(.*[0-9]){3,5}$/;
    var fiveToNineNumeric = /(.*[0-9]){5,9}/;
    var sixNumeric = /^[0-9]{6}$/;
    var sevenNumeric = /^[0-9]{7}$/;
    var sevenToNineNumeric = /^[0-9]{7,9}$/;
    var eightAreNumbers = /(.*[0-9]){8}/;
    var nineNumeric = /^[0-9]{9}$/;
    var nineAlphaChars = /^[A-Za-z0-9]{9}$/;
    var tenNumeric = /^[0-9]{10}$/;
    var elevenNumeric = /^.[0-9]{11}$/;
    var twelveNumeric = /^.[0-9]{12}$/;
    var hPlusEight = /([H][0-9]{8})$/;
    var sevenPlusX = /([H][0-9]{7}X)$/;

    if (stateCode == undefined || licenseNumber == undefined) {
        return "";
    }
    if (stateCode == 'AK') {
       return validateExpression(oneToSevenNumeric, licenseNumber, "Must be 1-7 numeric");
    }
    if (stateCode == 'AL') {
      return validateExpression(sevenNumeric, licenseNumber, "Must be 7 numeric");
    }
    if (stateCode == 'AR' || stateCode == 'MS') {
      return validateExpression(nineNumeric, licenseNumber, "Must be 9 numeric");
    }
    if (stateCode == 'AZ') { 
        if (nineNumeric.test(licenseNumber) == true) {
            return "";
        }
        if (oneAlpha.test(licenseNumber) && eightAreNumbers.test(licenseNumber)) {
            return "";
        }
        if (twoAlpha.test(licenseNumber) && threeToFiveNumeric.test(licenseNumber) && licenseNumber.length < 8) {
            return "";
        }
        return "Must be 1 Alphabetic, 8 Numeric; or 2 Alphabetic, 3-6 Numeric; or 9 Numeric";
    }
    if (stateCode == 'CA') {

        if (oneAlpha.test(licenseNumber) && oneAlphaPlusSeven.test(licenseNumber)) {
            return "";
        }
        return "Must be 1 alpha and 7 numeric";
    }
    if (stateCode == 'CO' || stateCode == 'CN' || stateCode == 'CT') {
        return validateExpression(nineNumeric, licenseNumber, "Must be 9 numeric");
    }
    if (stateCode == 'DC') {
        if (sevenNumeric.test(licenseNumber) || nineNumeric.test(licenseNumber)) {
            return "";
        }
        return  "Must be 7 - 9 numeric";
    }
    if (stateCode == 'DE') {
        if (oneToSevenNumeric.test(licenseNumber)) {
            return "";
        }
        return "Must be 1 - 7 numeric";
    }
    if (stateCode == 'FL') {
        if (oneAlpha.test(licenseNumber) && twelveNumeric.test(licenseNumber)) {
            return "";
        }
        return "Must be 1 alpha, 12 numeric";
    }
    if (stateCode == 'GA') {
        if (sevenToNineNumeric.test(licenseNumber)) {
            return "";
        }
        return "Must be 7 - 9 numeric";
    }
    if (stateCode == 'HI') {
        if (nineNumeric.test(licenseNumber) || hPlusEight.test(licenseNumber)) {
            return "";
        }
        return "Must 'H' + 8 numeric; 9 numeric";
    }
    if (stateCode == 'ID') {
        if (nineNumeric.test(licenseNumber) || sixNumeric.test(licenseNumber) || (twoAlpha.test(licenseNumber) && alphaPlusSixNumeric.test(licenseNumber))) {
            return "";
        }
        return "Must 9 numbers or 6 numbers; or 2 char, 6 numbers ";
    }

    if (stateCode == 'IL') {
        if (oneAlpha.test(licenseNumber) && elevenNumeric.test(licenseNumber)) {
            return "";
        }
        return "Must 1 character 11 numbers";
    }
    if (stateCode == 'IN') {
        if (tenNumeric.test(licenseNumber) || nineNumeric.test(licenseNumber)) {
            return "";
        }
        return "Must be 9-10 numbers";
    }
    if (stateCode == 'IA') {
        if (nineAlphaChars.test(licenseNumber) || nineNumeric.test(licenseNumber)) {
            return "";
        }
        return "Must be 9 alpha numbers";
    }
    if (stateCode == 'KS' || stateCode == 'KY') {
        if (nineNumeric.test(licenseNumber) || (oneAlpha.test(licenseNumber) && eightAreNumbers.test(licenseNumber) && licenseNumber.length == 9)) {
            return "";
        }
        return "Must be 1 alpha and 8 numeric";
    }
    if (stateCode == 'LA') {
        if (nineNumeric.test(licenseNumber) == true) {
            return "";
        }
        return "Must be 9 numeric";
    }
    if (stateCode == 'ME') {
        if (sevenNumeric.test(licenseNumber) || sevenPlusX.test(licenseNumber)) {
            return "";
        }
        return "Must be 7 numeric";
    }
    if (stateCode == 'MD' || stateCode == 'MI' || stateCode == 'MN') {
        if (oneAlpha.test(licenseNumber) && twelveNumeric.test(licenseNumber)) {
            return "";
        }
        return "1 Alphabetic, 12 Numeric";
    }
    if (stateCode == 'MA') {
        if ((oneAlpha.test(licenseNumber) && eightAreNumbers.test(licenseNumber) && licenseNumber.length == 9) || nineNumeric.test(licenseNumber)) {
            return "";
        }
        return "Must be 1 alpha, 8 numeric; 9 numeric";
    }

    if (stateCode == 'MO') {
        if ((oneAlpha.test(licenseNumber) && fiveToNineNumeric.test(licenseNumber) && licenseNumber.length < 11) || nineNumeric.test(licenseNumber)) {
            return "";
        }
        return "1 alpha - 5-9 Numeric or 9 numeric";
    }
    return "";
}

function validateExpression(expression, value, error) {
    if (expression.test(value) == true) {
        return "";
    }
    return error;
}

return {
    validateLicense: validateLicense,
    validateExpression: validateExpression
};

}

like image 117
James Fleming Avatar answered Oct 04 '22 01:10

James Fleming


I appreciated James' answer above, because it actually includes regex to answer this question, but it seems the drivers license formats he used are a little outdated in 2020. Here's the updated regex, if anyone needs it:

stateCode = $(<your-state-code-field>).children("option:selected").val();     
licenseNumber = $(<your-license-number-field>).attr("value");

// from https://ntsi.com/drivers-license-format/
// I opted only to set these three variables, because none of the other patterns are repeated
var sevenNumeric = /^[0-9]{7}$/;
var eightNumeric = /^[0-9]{8}$/;
var nineNumeric  = /^[0-9]{9}$/;

if (licenseNumber != 0 && (licenseNumber == "" || licenseNumber == 'null' || licenseNumber == 'undefined' || licenseNumber == false)) {
    error = "This field is required.";
} else if (stateCode == 'AK' || stateCode == 'DE') {
    if (!/^[0-9]{1,7}$/.test(licenseNumber)) {
        error = "Must be 1 to 7 numeric.";
    }
} else if (stateCode == 'AL') {
    if (!/^[0-9]{1,8}$/.test(licenseNumber)) {
        error = "Must be 1 to 8 numeric.";
    }
} else if (stateCode == 'AR') {
  if (!/^([0-9]){4,9}$/.test(licenseNumber)) {
       error = "Must be 4 to 9 numeric.";
  }
} else if (stateCode == 'AZ') { 
    if (!/^[A-Za-z]{1}[0-9]{8}$/.test(licenseNumber) && !nineNumeric.test(licenseNumber)) {
        error = "Must be 1 alphabetic and 8 numeric or 9 numeric.";
    }
} else if (stateCode == 'CA') {
    if (!/^[A-Za-z]{1}[0-9]{7}$/.test(licenseNumber)) {
        error = "Must be 1 alphabetic and 7 numeric.";
    }
} else if (stateCode == 'CO') {
  if (!(nineNumeric.test(licenseNumber)) && !(/^[A-Za-z]{1,2}[0-9]{3,5}$/.test(licenseNumber) && licenseNumber.length >= 4 ) ) {
       error = "Must be 9 numeric, or one alpha and 3 to 5 numeric, or 2 alpha and 2 to 5 numeric.";
  }
  
} else if (stateCode == 'CN' || stateCode == 'CT' || stateCode == 'MS') {
    if (!nineNumeric.test(licenseNumber)) {
        error = "Must be 9 numeric.";
    }
} else if (stateCode == 'DC' || stateCode == 'TN') {
    if (!sevenNumeric.test(licenseNumber) && !nineNumeric.test(licenseNumber)) {
        error = "Must be 7 or 9 numeric.";
    }
} else if (stateCode == 'FL') {
    if (!/^[A-Za-z]{1}[0-9]{12}$/.test(licenseNumber)) {
        error = "Must be 1 alpha, 12 numeric.";
    }
} else if (stateCode == 'GA') {
    if (!/^[0-9]{7,9}$/.test(licenseNumber)) {
        error = "Must be 7 to 9 numeric.";
    }
} else if (stateCode == 'HI') {
    if (!(/[0-9]{8}$/.test(licenseNumber) && licenseNumber.length == 9)) {
        error = "Must have 1 alpha and 8 numeric, or 9 numeric.";
    }
} else if (stateCode == 'ID') {
    if ( !nineNumeric.test(licenseNumber) && !/[A-Za-z]{2}[0-9]{7}[A-Za-z]{1}$/.test(licenseNumber) ) {
        error = "Must have 9 numeric, or 2 alphabetic + 6 numeric + 1 alpha.";
    }
} else if (stateCode == 'IL') {
    if ( !/^[A-Za-z]{1}[0-9]{11,12}$/.test(licenseNumber) ) {
        error = "Must be 1 alphabetic 11 numeric, or 1 alpha and 12 numeric.";
    }
} else if (stateCode == 'IN') {
  if (!(/^[A-Za-z]{0,1}[0-9]{9,10}$/.test(licenseNumber) && licenseNumber.length <= 10)) {
        error = "Must be one alpha and 9 numeric, or 9 to 10 numeric.";
    }
} else if (stateCode == 'IA') {
    if (!nineNumeric.test(licenseNumber) && !/^[0-9]{3}[A-Za-z]{2}[0-9]{4}$/.test(licenseNumber) ) {
        error = "Must be 9 numeric, or 3 numeric + 2 alpha + 4 numeric.";
    }
} else if (stateCode == 'KS') {
    if (!/^[A-Za-z]{1}[0-9]{1}[A-Za-z]{1}[0-9]{1}[A-Za-z]{1}$/.test(licenseNumber) && !( /^[A-Za-z]{0,1}[0-9]{8}$/.test(licenseNumber) && licenseNumber.length == 9 )) {
        error = "Must be alpha + number + alpha + number + alpha, or 1 alpha and 8 numeric, or 9 numeric.";
    }
} else if (stateCode == 'KY') {
    if (!/^[A-Za-z]{1}[0-9]{8,9}$/.test(licenseNumber) && !nineNumeric.test(licenseNumber) ) {
        error = "Must be 1 alphabetic and 8 to 9 numeric, or 9 numeric.";
    }
} else if (stateCode == 'LA' || stateCode == 'OR') {
    if (!/^[0-9]{1,9}$/.test(licenseNumber)) {
        error = "Must be 1 to 9 numeric.";
    }
} else if (stateCode == 'ME') {
    if ( !( /^[0-9]{7,8}[A-Za-z]{0,1}$/.test(licenseNumber) && licenseNumber.length <= 8)) {
        error = "Must be 7 numeric, 7 numeric and one alpha, or 8 numeric.";
    }
} else if (stateCode == 'MD' || stateCode == 'MN') {
    if (!/^[A-Za-z]{1}[0-9]{12}$/.test(licenseNumber)) {
        error = "Must be 1 alphabetic and 12 numeric.";
    }
} else if (stateCode == 'MA') {
    if (!(/[0-9]{8}$/.test(licenseNumber) && licenseNumber.length == 9)) {
        error = "Must be 1 alphabetic and 8 numeric, or 9 numeric.";
    }
} else if (stateCode == 'MI') {
   if (!(/^[A-Za-z]{1}[0-9]{10,12}$/.test(licenseNumber) && licenseNumber.length != 12)) {
     error = "Must be 1 alpha and 10 numeric, or 1 alpha and 12 numeric.";
   }
} else if (stateCode == 'MT') {
    if (!/^[A-Za-z]{1}[0-9]{8}$/.test(licenseNumber) && !nineNumeric.test(licenseNumber) && !/^[0-9]{13,14}$/.test(licenseNumber) ) {
        error = "Must have one alpha and 8 numeric, or 9, 13, or 14 numeric.";
    }
} else if (stateCode == 'NE') {
    if (!/^[A-Za-z]{1}[0-9]{6,8}$/.test(licenseNumber)) {
        error = "Must have one alpha and six to eight numeric.";
    }
} else if (stateCode == 'NV') {
    if (!(/^[0-9]{9,12}$/.test(licenseNumber) && licenseNumber.length != 11) && !/^[0-9]{8}[Xx]$/.test(licenseNumber)) {
        error = "Must have 9, 10, or 12 numeric, or eight numeric + X.";
    }
} else if (stateCode == 'NH') {
    if (!/^[0-9]{2}[A-Za-z]{3}[0-9]{5}$/.test(licenseNumber)) {
        error = "Must have 2 numeric + 3 alpha + 5 numeric.";
    }
} else if (stateCode == 'NJ') {
    if (!/^[A-Za-z]{1}[0-9]{14}$/.test(licenseNumber)) {
        error = "Must have one alpha and 14 numeric.";
    }
} else if (stateCode == 'NM') {
    if (!/^[0-9]{8,9}$/.test(licenseNumber)) {
        error = "Must be 8 or 9 numeric.";
    }
} else if (stateCode == 'NY') {
    if (!/^[A-Za-z]{1}[0-9]{7}$/.test(licenseNumber) && !/^[A-Za-z]{1}[0-9]{18}$/.test(licenseNumber) && !eightNumeric.test(licenseNumber) && !nineNumeric.test(licenseNumber) && !/^[A-Za-z]{8}$/.test(licenseNumber) ) {
        error = "Must have one alpha and 7 or 18 numeric, 8 or 9 numeric, or 8 alpha.";
    }    
} else if (stateCode == 'NC') {
    if (!/^[0-9]{1,12}$/.test(licenseNumber)) {
        error = "Must have 1 to 12 numeric.";
    }
} else if (stateCode == 'ND') {
    if (!/^[A-Za-z]{3}[0-9]{6}$/.test(licenseNumber) && !nineNumeric.test(licenseNumber) ) {
        error = "Must have 3 alpha and 6 numeric, or 9 numeric.";
    }
} else if (stateCode == 'OH') {
    if (!(/^[A-Za-z]{1,2}[0-9]{3,8}$/.test(licenseNumber) && licenseNumber.length >= 5 && licenseNumber.length <= 9) && !eightNumeric.test(licenseNumber) ) {
        error = "Must have one alpha and 4 to 8 numeric, 2 alpha and 3 to 7 numeric, or 8 numeric.";
    }
} else if (stateCode == 'OK') {
    if (!/^[A-Za-z]{0,1}[0-9]{9}$/.test(licenseNumber)) {
        error = "Must have one alpha and 9 numeric, or 9 numeric.";
    }
} else if (stateCode == 'PA') {
    if (!eightNumeric.test(licenseNumber)) {
        error = "Must have 8 numeric.";
    }
} else if (stateCode == 'RI') {
    if (!/^[0-9]{7}[A-Za-z]{1}[0-9]{6}$/.test(licenseNumber)) {
        error = "Must have 7 numeric + 1 alpha + 6 numeric.";
    }
} else if (stateCode == 'SC') {
    if (!/(^[0-9]){5,11}$/.test(licenseNumber)) {
        error = "Must have 5 to 11 numeric.";
    }
} else if (stateCode == 'SD') {
    if (!(/(.*[0-9]){6,10}/.test(licenseNumber) && licenseNumber.length != 11) ) {
        error = "Must have 6 to 10 numeric or 12 numeric.";
    }
} else if (stateCode == 'TX') {
    if (!/^[0-9]{7,8}$/.test(licenseNumber)) {
        error = "Must have 7 to 8 numeric.";
    }
} else if (stateCode == 'UT') {
    if (!/(^[0-9]){4,10}$/.test(licenseNumber)) {
        error = "Must have 4 to 10 numeric.";
    }
} else if (stateCode == 'VT') {
    if (!eightNumeric.test(licenseNumber) && ! /^([0-9]{7}[Aa])$/.test(licenseNumber)) {
        error = "Must have 8 numeric or 7 numeric plus 'A'.";
    }
} else if (stateCode == 'VI') {
    if (!/^[A-Za-z]{1}[0-9]{8,11}$/.test(licenseNumber) && !nineNumeric.test(licenseNumber)) {
        error = "Must be 1 alpha and eight to 11 numeric, or 9 numeric.";
    }
} else if (stateCode == 'WA') { //1-7Alpha+any combination of Alpha, Numeric, or * for a total of 12 characters
    if (!(/^[A-Za-z]{1,7}/.test(licenseNumber) && licenseNumber.length == 12)) {
        error = "Must be 1 to 7 alpha and total 12 characters.";
    }
} else if (stateCode == 'WV') {
    if (!sevenNumeric.test(licenseNumber) && !/^[A-Za-z]{1,2}[0-9]{5,6}$/.test(licenseNumber) ) {
        error = "Must be 7 numeric, or 1 to 2 alpha and 5 to 6 numeric.";
    }
} else if (stateCode == 'WI') {
    if (!/^[A-Za-z]{1}[0-9]{13}$/.test(licenseNumber)) {
        error = "Must be 1 alpha and 13 numeric.";
    }
} else if (stateCode == 'WY') {
    if (!/^[0-9]{9,10}$/.test(licenseNumber)) {
        error = "Must be 9 to 10 numeric.";
    }
} else { // if (stateCode == 'MO')
    if (!/^[A-Za-z]{1}[0-9]{5,9}$/.test(licenseNumber) && !(/^[0-9]{8,9}[A-Za-z]{1,2}$/.test(licenseNumber) && licenseNumber.length == 10) && !nineNumeric.test(licenseNumber) && !/^[A-Za-z]{1}[0-9]{6}[Rr]$/.test(licenseNumber)) {
        error = "Must be 1 alphabetic and 5-9 numeric, eight numeric and two alpha, or 9 numeric and one alpha, or 9 numeric, or 1 alpha + 9 numeric + R.";
    }
}
// update the error message, and prevent user from continuing if error is present
if (error != "") {
  $(<your-error-field>).text(error);
   return false;
} else {
   return true;
}

Assuming your form looks something like this:

<form name="customer-form" action="" method="post">
    <select id="<your-state-code-field>">
      <option value="CA">CA</option>
      <option value="NV">NV</option>
      <option value="ME">ME</option>
      ...
    </select>
    <input type="text" id="<your-license-number-field>" name="drivers-license">
    <div id="<your-error-field>" class="error-msg"></div>
    <input type="submit" value="Submit" >
</form>

I'm no regex or license expert, so if I missed anything, or could improve this code in any way let me know.

like image 25
Jillian Hoenig Avatar answered Oct 03 '22 23:10

Jillian Hoenig