Validating an Australian drivers license number using Regex

The following code snippet demonstrates validation of an Australian drivers license using Regex.

Here are the rules for the license number format,

1. Max length 9 characters

2. Alphanumeric characters only

3. Must have at least 4 numeric characters

4. Must have no more than 2 alphabetic characters

5. The third and fourth character must be numeric

This is the Regex way to validate this,


public bool ValidateLicenseNumber(string licenseNumber)

{

bool result = false;

result =

Regex.IsMatch(licenseNumber, @"^[A-Za-z0-9]{4,9}$") // Rule 1 and 2

&& Regex.IsMatch(licenseNumber, "^..[0-9]{2}") // rule 5

&& Regex.IsMatch(licenseNumber, "(.*[0-9]){4}") // Rule 3

&& !Regex.IsMatch(licenseNumber, "(.*[A-Za-z]){3}"); // Rule 4

return result;

}

Have fun.

Tags: , ,

Leave a Reply