验证驾驶执照号码?

时间:2010-07-26 19:26:23

标签: c# jquery validation

我正在研究ACH支付处理器,并想知道我是否可以根据某些规则确定驱动程序许可证字段的范围。有什么想法吗?我可以只假设数字或其他更像SSN

的数字

由于

5 个答案:

答案 0 :(得分:7)

请参阅ADR State License Formats(PDF)。

答案 1 :(得分:7)

我想出了一个我正在做的项目...

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
};

}

答案 2 :(得分:2)

我赞赏詹姆斯在上面的回答,因为它实际上包含了正则表达式来回答这个问题,但是看来他在2020年使用的驾驶执照格式有些过时。如果有人需要,这是更新的正则表达式:

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;
}

假设您的表单看起来像这样:

<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>

我不是正则表达式或许可证专家,所以如果我错过了任何内容,或者可以通过任何方式改进此代码,请告诉我。

答案 3 :(得分:0)

假设您在美国各个州的驾驶执照号码格式不同,所以到目前为止我知道您将在州范围内进行验证。这个网站应该通过驾驶执照号码验证你的身份:

http://www.highprogrammer.com/alan/numbers/index.html

答案 4 :(得分:0)

这是我根据 https://ntsi.com/drivers-license-format/ 建议编写的 PHP 实现。这已经过单元测试。

/**
 * Based on https://ntsi.com/drivers-license-format/
 * Does not provide validation for territories such as Guam, American Samoa, etc
 * @param $license string
 * @param $state string Lowercase, two character state abbreviation
 * @param bool $returnFalseOnUnmatchedState
 * @return bool
 */
 function isValidLicense(string $license, string $state, $returnFalseOnUnmatchedState = true): bool
{
    $license = strtolower($license);
    $state = strtoupper($state);

    $sevenNumeric = '~^[0-9]{7}$~';
    $eightNumeric = '~^[0-9]{8}$~';
    $nineNumeric  = '~^[0-9]{9}$~';

    if (!$license) {
        return false;
    }

    switch ($state) {
        case 'AK': // Alaska
            // 1-7 Numeric
            return (bool)preg_match('~^[0-9]{1,7}$~', $license);
        case 'AL': // Alabama
            // 1-8 Numeric
            return (bool)preg_match('~^[0-9]{1,8}$~', $license);
        case 'AR': // Arkansas
            // 4-9 Numeric
            return (bool)preg_match('~^[0-9]{4,9}$~', $license);
        case 'AZ': // Arizona
        case 'MA': // Massachusetts
            /**x
             * 1 Alpha + 8 Numeric or
             * 9 Numeric
             */
            return (
                preg_match('~^[a-z][0-9]{8}$~', $license) ||
                preg_match($nineNumeric, $license)
            );
        case 'CA': // California
            // 1 Alpha + 7 Numeric
            return (bool)preg_match('~^[a-z]{1}[0-9]{7}$~', $license);
        case 'CO': // Colorado
            /*
             * 9 Numeric or
             * 1 Alpha + 3-6 Numeric or
             * 2 Alpha + 2-5 Numeric
             */
            return (
                preg_match($nineNumeric, $license) ||
                preg_match('~^[a-z]{1}[0-9]{3,6}$~', $license) ||
                preg_match('~^[a-z]{2}[0-9]{2,5}$~', $license)
            );
        case 'CT': // Conneticut
        case 'MS': // Mississippi
            // 9 Numeric
            return (bool)preg_match($nineNumeric, $license);
        case 'DC': // District of Columbia
        case 'TN': // Tennessee
            /*
             * 7 Numeric or
             * 9 Numeric
             */
            return (
                preg_match($nineNumeric, $license) ||
                preg_match($sevenNumeric, $license)
            );
        case 'DE': // Deleware
            // 1-7 Numeric
            return (bool)preg_match('~^[0-9]{1,7}$~', $license);
        case 'FL': // Florida
        case 'MN': // Minnesota
        case 'MD': // Maryland
            // 1 Alpha + 12 Numeric
            return (bool)preg_match('~^[a-z][0-9]{12}$~', $license);
        case 'GA': // Georgia
            // 7-9 Numeric
            return (bool)preg_match('~^[0-9]{7,9}$~', $license);
        case 'HI': // Hawaii
            /*
             * 1 Alpha + 8 Numeric or
             * 9 Numeric
             */
            return (
                preg_match('~^[a-z][0-9]{8}$~', $license) ||
                preg_match($nineNumeric, $license)
            );
        case 'IA': // Iowa
            /*
             * 9 Numeric or
             * 3 Numeric + 2 Alpha + 4 Numeric
             */
            return (
                preg_match($nineNumeric, $license) ||
                preg_match('~^[0-9]{3}[a-z]{2}[0-9]{4}$~', $license)
            );
        case 'ID': // Idaho
            /*
             * 2 Alpha + 6 Numeric + 1 Alpha or
             * 9 Numeric
             */
            return (
                preg_match('~^[a-z]{2}[0-9]{6}[a-z]$~', $license) ||
                preg_match($nineNumeric, $license)
            );
        case 'IL': // Illinois
            /*
             * 1 Alpha + 11 Numeric or
             * 1 Alpha + 12 Numeric
             */
            return (bool)preg_match('~^[a-z][0-9]{11,12}$~', $license);
        case 'IN': // Indiana
            /*
             * 1 Alpha + 9 Numeric or
             * 9 Numeric or
             * 10 Numeric
             */
            return (
                preg_match('~^[a-z]{1}[0-9]{9}$~', $license) ||
                preg_match('~^[0-9]{9,10}$~', $license)
            );
        case 'KS': // Kansas
            /*
             * 1 Alpha + 1 Numeric + 1 Alpha + 1 Numeric + 1 Alpha or
             * 1 Alpha + 8 Numeric or
             * 9 Numeric
             */
            return (
                preg_match('~^[A-Za-z]{1}[0-9]{1}[A-Za-z]{1}[0-9]{1}[A-Za-z]{1}$~', $license) ||
                preg_match('~^[A-Za-z]{0,1}[0-9]{8}$~', $license) ||
                preg_match($nineNumeric, $license)
            );
        case 'KY': // Kentucky
            /*
             * 1 Alpha + 8 Numeric or
             * 1 Alpha + 9 Numeric or
             * 9 Numeric
             */
            return (
                preg_match('~^[a-z]{1}[0-9]{8,9}$~', $license) ||
                preg_match($nineNumeric, $license)
            );
        case 'LA': // Louisiana
        case 'OR': // Oregon
            // 1-9 Numeric
            return (bool)preg_match('~^[0-9]{1,9}$~', $license);
        case 'ME': // Maine
            /*
             * 7 Numeric or
             * 7 Numeric + 1 Alpha or
             * 8 Numeric
             */
            return (
                preg_match('~^[0-9]{7}[a-z]{1}$~', $license) ||
                preg_match($sevenNumeric, $license) ||
                preg_match($eightNumeric, $license)
            );
        case 'MI': // Michigan
            /*
             * 1 Alpha + 10 Numeric or
             * 1 Alpha + 12 Numeric
             */
            return (
                preg_match('~^[a-z]{1}[0-9]{10}$~', $license) ||
                preg_match('~^[a-z]{1}[0-9]{12}$~', $license)
            );
        case 'MO': // Missouri
            /*
             * 1 Alpha + 5-9 Numeric or
             * 1 Alpha + 6 Numeric + R or
             * 8 Numeric + 2 Alpha or
             * 9 Numeric + 1 Alpha or
             * 9 Numeric
             */
            return (
                preg_match('~^[a-z][0-9]{5,9}$~', $license) ||
                preg_match('~^[a-z][0-9]{6}r$~', $license) ||
                preg_match('~^[0-9]{8}[a-z]{2}$~', $license) ||
                preg_match('~^[0-9]{9}[a-z]{1}$~', $license) ||
                preg_match($nineNumeric, $license)
            );
        case 'MT': // Montana
            /*
             * 1 Alpha + 8 Numeric or
             * 9 Numeric or
             * 13 Numeric or
             * 14 Numeric
             */
            return (bool)(
                preg_match('~^[A-Za-z]{1}[0-9]{8}$~', $license) ||
                preg_match($nineNumeric, $license) ||
                preg_match('~^[0-9]{13}$~', $license) ||
                preg_match('~^[0-9]{14}$~', $license)
            );
        case 'NC': // North Carolina
            // 1-12 Numeric
            return (bool)preg_match('~^[0-9]{1,12}$~', $license);
        case 'ND': // North Dakota
            /*
             * 3 Alpha + 6 Numeric or
             * 9 Numeric
             */
            return (
                preg_match('~^[a-z]{3}[0-9]{6}$~', $license) ||
                preg_match($nineNumeric, $license)
            );
        case 'NE': // Nebraska
            // 1 Alpha + 6-8 Numeric
            return (bool)preg_match('~^[A-Za-z]{1}[0-9]{6,8}$~', $license);
        case 'NH': // New Hampshire
            // 2 Numeric + 3 Alpha + 5 Numeric
            return (bool)preg_match('~^[0-9]{2}[a-z]{3}[0-9]{5}$~', $license);
        case 'NJ': // New Jersy
            // 1 Alpha + 14 Numeric
            return (bool)preg_match('~^[a-z]{1}[0-9]{14}$~', $license);
        case 'NM': // New Mexico
            // 8-9 Numeric
            return (bool)preg_match('~^[0-9]{8,9}$~', $license);
        case 'NV': // Nevada
            /*
             * 9 Numeric or
             * 10 Numeric or
             * 12 Numeric or
             * X + 8 Numeric
             */
            return (bool)(
                preg_match($nineNumeric, $license) ||
                preg_match('~^[0-9]{10}$~', $license) ||
                preg_match('~^[0-9]{12}$~', $license) ||
                preg_match('~^x[0-9]{8}$~', $license)
            );
        case 'NY': // New York
            /*
             * 1 Alpha + 7 Numeric or
             * 1 Alpha + 18 Numeric or
             * 8 Numeric or
             * 9 Numeric or
             * 16 Numeric or
             * 8 Alpha
             */
            return (bool)(
                preg_match('~^[a-z][0-9]{7}$~', $license) ||
                preg_match('~^[a-z][0-9]{18}$~', $license) ||
                preg_match($eightNumeric, $license) ||
                preg_match($nineNumeric, $license) ||
                preg_match('~^[0-9]{16}$~', $license) ||
                preg_match('~^[a-z]{8}$~', $license)
            );
        case 'OH': // Ohio
            /*
             * 1 Alpha + 4-8 Numeric or
             * 2 Alpha + 3-7 Numeric or
             * 8 Numeric
             */
            return (
                preg_match('~^[a-z][0-9]{4,8}$~', $license) ||
                preg_match('~^[a-z]{2}[0-9]{3,7}$~', $license) ||
                preg_match($eightNumeric, $license)
            );
        case 'OK': // Oklahoma
            /*
             * 1 Alpha + 9 Numeric or
             * 9 Numeric
             */
            return (
                preg_match('~^[a-z][0-9]{9}$~', $license) ||
                preg_match($nineNumeric, $license)
            );
        case 'PA': // Pennsylvania
            // 8 Numeric
            return (bool)preg_match($eightNumeric, $license);
        case 'RI': // Rhode Island
            /*
             * 7 Numeric or
             * 1 Alpha + 6 Numeric
             */
            return (
                preg_match($sevenNumeric, $license) ||
                preg_match('~^[a-z][0-9]{6}$~', $license)
            );
        case 'SC': // South Carolina
            // 5-11 Numeric
            return (bool)preg_match('~^[0-9]{5,11}$~', $license);
        case 'SD': // South Dakota
            /*
             * 6-10 Numeric or
             * 12 Numeric
             */
            return (
                preg_match('~^[0-9]{6,10}$~', $license) ||
                preg_match('~^[0-9]{12}$~', $license)
            );
        case 'TX': // Texas
            // 7-8 Numeric
            return (bool)preg_match('~^[0-9]{7,8}$~', $license);
        case 'UT': // Utah
            // 4-10 Numeric
            return (bool)preg_match('~^[0-9]{4,10}$~', $license);
        case 'VT': // Vermont
            /*
             * 8 Numeric or
             * 7 Numeric+A
             */
            return (
                preg_match($eightNumeric, $license) ||
                preg_match('~^[0-9]{7}a$~', $license)
            );
        case 'VA': // Virginia
            /*
             * 1 Alpha + 8-11 Numeric or
             * 9 Numeric
             */
            return (
                preg_match('~^[a-z][0-9]{8,11}$~', $license) ||
                preg_match($nineNumeric, $license)
            );
        case 'WA': // Washington
            // 1-7 Alpha + any combination of Alpha, Numeric, or * for a total of 12 characters
            return (
                preg_match('~^[a-z]{1,7}[a-z0-9*]{5,11}$~', $license)
                && strlen($license) == 12
            );
        case 'WV': // West Virginia
            /*
             * 7 Numeric or
             * 1-2 Alpha + 5-6 Numeric
             */
            return (
                preg_match($sevenNumeric, $license) ||
                preg_match('~^[a-z]{1,2}[0-9]{5,6}$~', $license)
            );
        case 'WI': // Wisconsin
            // 1 Alpha + 13 Numeric
            return (bool)preg_match('~^[a-z][0-9]{13}$~', $license);
        case 'WY': // Wyoming
            // 9 - 10 Numeric
            return (bool)preg_match('~^[0-9]{9,10}$~', $license);
        default:
            return !$returnFalseOnUnmatchedState;
    }
}

请注意@Jillian Hoenig 的回答在很多地方都有不正确的验证。我没有足够的代表,否则我会评论并警告其他用户。