Validate Drivers License Numbers

Help with drivers license number validation regex

Trying to solve this with just one regex is probably a little hard as you need to keep track of multiple things. I'd suggest you try validating each of the properties separately (unless it makes sense to do otherwise).

For example you can verify the first and second properties easily by checking for a character class including all alphanumeric characters and a quantifier which tells that it should be present at most 9 times:

^[0-9a-zA-Z]{4,9}$

The anchors ^ and $ ensure that this will, in fact, match the entire string and not just a part of it. As Marc Gravell pointed out the string "aaaaa" will match the expression "a{3}" because it can match partially as well.

Checking the fifth property can be done similarly, although this time we don't care about the rest:

^..[0-9]{2}

Here the ^ character is an anchor for the start of the string, the dot (.) is a placeholder for an arbitrary character and we're checking for the third and fourth character being numeric again with a character class and a repetition quantifier.

Properties three and four are probably easiest validated by iterating through the string and keeping counters as you go along.

EDIT: Marc Gravell has a very nice solution for those two cases with regular expressions as well. Didn't think of those.

If you absolutely need to do this in one regular expression this will be a bit work (and probably neither faster nor more readable). Basically I'd start with enumerating all possible options such a string could look like. I am using a here as placeholder for an alphabetic characters and 1 as a placeholder for a number.

We need at least four characters (3) and the third and fourth are always fixed as numbers. For four-character strings this leaves us with only one option:

1111

Five-character strings may introduce a letter, though, with different placements:

a1111
1a111
1111a

and, as before, the all-numeric variant:

11111

Going on like this you can probably create special rules for each case (basically I'd divide this into "no letter", "one letter" and "two letters" and enumerate the different patterns for that. You can then string together all patterns with the pipe (|) character which is used as an alternative in regular expressions.

Convert this in validation expression driver's license numbers

Here's a regular expression editor. It's aimed towards Ruby but will do for .NET as well:

  • http://rubular.com/

I don't know about the detailed specification of the license numbers you#re looking for, but I created a regex based on your example: ^[A-Z]\d{2}-\d{2}-\d{6}$.

You can modify it here:

  • http://rubular.com/r/7bHsX1tJ23

The example explained:

^[A-Z]\d{2}-\d{2}-\d{6}$

^ = start of line

[A-Z] = a single upper case letter

\d{2} = any number with 2 digits

\d{6} = any number with 6 digits

$ = end of line

If you want to make sure you don't miss lower case letters starting the license use [A-Za-z] instead of [A-Z]

(Thanks to Paul Sullivan)

Regex pattern for Driving Licence validation

You can use the regular expression

^[A-Z](?:\d[- ]*){14}$

Demo

The regex engine performs the following operations.

^        # match beginning of line
[A-Z] # match a capital letter
(?: # begin a non-cap grp
\d # match a digit
[- ]* # match a hypthen or space 0+ times
) # end non-cap grp
{14} # execute non-cap grp 14 times
$ # match end of line

License Number Validation in JavaScript

You can't do string array indexing like chkVal38[0] in older versions of IE.

I would suggest this cleaned up and simplified code:

//license number
var strFilter = /^[0-3][0-9]{7}$/;
var obj = document.getElementById("license");

if (!strFilter.test(obj.value)) {
alert("Please enter valid 8-digit license number\r\n(Only digits)");
obj.focus();
obj.style.background = "#DFE32D";
obj.value = "";
return false;
}

Working example here: http://jsfiddle.net/jfriend00/ZUajd/

Changes:

  • Moved all the validation into the single regex (leading 0-3, all numbers and 8 total chars).
  • Retrieved the DOM object once and then use that reference rather than retrieving it every time
  • Clear the field by setting to "", not null.

Validate UK Driving license regex in javascript

This should work:

^[A-Z9]{5}\d{6}[A-Z9]{2}\d[A-Z]{2}$

Explanation:

  • [A-Z9]{5} Digit 1–5: Letters A-Z or 9's
  • \d{6} Digit 6-11: Numbers 0-9
  • [A-Z9]{2} Digit 12–13: Letters A-Z or 9's
  • \d Digit 14: Number 0-9
  • [A-Z]{2} Digit 15–16: Two letters A-Z

var regex = /^^[A-Z9]{5}\d{6}[A-Z9]{2}\d[A-Z]{2}$$/
console.log('all pass'); [ 'FARME100165AB5EW', 'MAR99740614BC2TL', 'MARTI740614A92TL', 'MARTI740614992TL', '99999740614992TL' ].forEach(drivingLicenceNumber => { console.log(drivingLicenceNumber, regex.test(drivingLicenceNumber)); }); console.log('all fail'); [ '1FARM382940AZ9AZ', 'F2ARM382940AZ9AZ', 'FA3RM382940AZ9AZ', 'FAR4M382940AZ9AZ', 'FARM5382940AZ9AZ' ].forEach(drivingLicenceNumber => { console.log(drivingLicenceNumber, regex.test(drivingLicenceNumber)); });

Driver's License Verification in Flutter / Dart

Seems like there's no plugin for that yet, but since it can be done fairly the same way in most languages, it's been done already. Take a look: https://stackoverflow.com/a/29835561/6696558.

In Dart it would something like this:

class DriverLicenseValidator {
// From http://www.uiia.org/documents/license_guidelines_08.pdf
static final oneToSevenNumeric = RegExp(r'^[0-9]{1,7}$');
static final oneAlpha = RegExp(r'(.*[A-Za-z]){1}');
static final oneAlphaPlusSeven = RegExp(r'^.[0-9]{7}$');
static final twoAlpha = RegExp(r'(.*[A-Za-z]){2}');
static final alphaPlusSixNumeric = RegExp(r'(.*[0-9]){6}$');
static final threeToFiveNumeric = RegExp(r'(.*[0-9]){3,5}$');
static final fiveToNineNumeric = RegExp(r'(.*[0-9]){5,9}');
static final sixNumeric = RegExp(r'^[0-9]{6}$');
static final sevenNumeric = RegExp(r'^[0-9]{7}$');
static final sevenToNineNumeric = RegExp(r'^[0-9]{7,9}$');
static final eightAreNumbers = RegExp(r'(.*[0-9]){8}');
static final nineNumeric = RegExp(r'^[0-9]{9}$');
static final nineAlphaChars = RegExp(r'^[A-Za-z0-9]{9}$');
static final tenNumeric = RegExp(r'^[0-9]{10}$');
static final elevenNumeric = RegExp(r'^.[0-9]{11}$');
static final twelveNumeric = RegExp(r'^.[0-9]{12}$');
static final hPlusEight = RegExp(r'([H][0-9]{8})$');
static final sevenPlusX = RegExp(r'([H][0-9]{7}X)$');

/// If there's no error, returns an empty [String]
/// If there's an error, returns an error [String]
static String checkForError(String stateCode, String licenseNumber) {
if (stateCode == null || licenseNumber == null) {
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.hasMatch(licenseNumber)) {
return "";
}

if (oneAlpha.hasMatch(licenseNumber) && eightAreNumbers.hasMatch(licenseNumber)) {
return "";
}

if (twoAlpha.hasMatch(licenseNumber) &&
threeToFiveNumeric.hasMatch(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.hasMatch(licenseNumber) && oneAlphaPlusSeven.hasMatch(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.hasMatch(licenseNumber) || nineNumeric.hasMatch(licenseNumber)) {
return "";
}

return "Must be 7 - 9 numeric";
}

if (stateCode == 'DE') {
if (oneToSevenNumeric.hasMatch(licenseNumber)) {
return "";
}

return "Must be 1 - 7 numeric";
}

if (stateCode == 'FL') {
if (oneAlpha.hasMatch(licenseNumber) && twelveNumeric.hasMatch(licenseNumber)) {
return "";
}

return "Must be 1 alpha, 12 numeric";
}

if (stateCode == 'GA') {
if (sevenToNineNumeric.hasMatch(licenseNumber)) {
return "";
}

return "Must be 7 - 9 numeric";
}

if (stateCode == 'HI') {
if (nineNumeric.hasMatch(licenseNumber) || hPlusEight.hasMatch(licenseNumber)) {
return "";
}

return "Must 'H' + 8 numeric; 9 numeric";
}

if (stateCode == 'ID') {
if (nineNumeric.hasMatch(licenseNumber) ||
sixNumeric.hasMatch(licenseNumber) ||
(twoAlpha.hasMatch(licenseNumber) && alphaPlusSixNumeric.hasMatch(licenseNumber))) {
return "";
}

return "Must 9 numbers or 6 numbers; or 2 char, 6 numbers ";
}

if (stateCode == 'IL') {
if (oneAlpha.hasMatch(licenseNumber) && elevenNumeric.hasMatch(licenseNumber)) {
return "";
}

return "Must 1 character 11 numbers";
}

if (stateCode == 'IN') {
if (tenNumeric.hasMatch(licenseNumber) || nineNumeric.hasMatch(licenseNumber)) {
return "";
}

return "Must be 9-10 numbers";
}

if (stateCode == 'IA') {
if (nineAlphaChars.hasMatch(licenseNumber) || nineNumeric.hasMatch(licenseNumber)) {
return "";
}

return "Must be 9 alpha numbers";
}

if (stateCode == 'KS' || stateCode == 'KY') {
if (nineNumeric.hasMatch(licenseNumber) ||
(oneAlpha.hasMatch(licenseNumber) &&
eightAreNumbers.hasMatch(licenseNumber) &&
licenseNumber.length == 9)) {
return "";
}

return "Must be 1 alpha and 8 numeric";
}

if (stateCode == 'LA') {
if (nineNumeric.hasMatch(licenseNumber) == true) {
return "";
}

return "Must be 9 numeric";
}

if (stateCode == 'ME') {
if (sevenNumeric.hasMatch(licenseNumber) || sevenPlusX.hasMatch(licenseNumber)) {
return "";
}

return "Must be 7 numeric";
}

if (stateCode == 'MD' || stateCode == 'MI' || stateCode == 'MN') {
if (oneAlpha.hasMatch(licenseNumber) && twelveNumeric.hasMatch(licenseNumber)) {
return "";
}

return "1 Alphabetic, 12 Numeric";
}

if (stateCode == 'MA') {
if ((oneAlpha.hasMatch(licenseNumber) &&
eightAreNumbers.hasMatch(licenseNumber) &&
licenseNumber.length == 9) ||
nineNumeric.hasMatch(licenseNumber)) {
return "";
}

return "Must be 1 alpha, 8 numeric; 9 numeric";
}

if (stateCode == 'MO') {
if ((oneAlpha.hasMatch(licenseNumber) &&
fiveToNineNumeric.hasMatch(licenseNumber) &&
licenseNumber.length < 11) ||
nineNumeric.hasMatch(licenseNumber)) {
return "";
}

return "1 alpha - 5-9 Numeric or 9 numeric";
}

return "Invalid state code";
}

static String _validateExpression(RegExp expression, String value, String error) =>
expression.hasMatch(value) ? "" : error;
}



Related Topics



Leave a reply



Submit