Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everyone,
I want to validate drivers license number using regular expression with following conditions:
1.First 2 alphabets.
2.Next 2 numeric.
3.Next 4 a valid year.
4.Remaining 7 numeric.

Thanks in advance.
Posted
Updated 16-Dec-15 18:20pm
v2
Comments
BillWoodruff 17-Dec-15 4:17am    
What have you tried so far ? Show your code.

The year is the problem - the rest of it is trivial.
I would do the year validation as a separate operation: extract it as part of the regex, then check it as a valid year in code rather than try to do it as a regular expression operation.
The reasons are twofold
1) Year validity changes every year!
2) If this is a birth year - which is likely- then it depends on a "reasonableness" test for maximum and minimum age, which again changes according to the current date.
And regular expressions are text matching processors, they don't deal with dates!

So to extract the bits is simple:
^(?<intro>[A-Z]{2})(?<numeric>\d{2})(?<year>\d{4})(?<tail>\d{7})$
 
Share this answer
 
Comments
Mitul Birla 17-Dec-15 2:43am    
Thanks for reply.
But above expression gives me following error:
parsing "^(?[A-Z]{2})(?[0-9]{2})(?[0-9]{4})(?[0-9]{7})$" - Unrecognized grouping construct.

Any help on this.
OriginalGriff 17-Dec-15 3:32am    
Probably because it's not what I wrote, and it's illegal grouping syntax...
Try my version, and use "named groups" where possible - it makes the code much more readable.
Mitul Birla 17-Dec-15 4:17am    
Yes, but I wrote that above code in c# so it gave me error.
Even in javascript the above written line dosen't work.
Following is the javascript code:


var re = '^(?[A-Z]{2})(?\d{2})(?\d{4})(?\d{7})$';
if (!re.test(num)) {
alert("Please enter valid Driving Licence.(e.g. MH4320120014306)"); $("#MainContent_txtPermitDrivingLicenceNumber").val("");
}
OriginalGriff 17-Dec-15 4:23am    
It doesn't matter what coding language you use: it's bad Regex syntax so it won't work!
Go here:
http://www.ultrapico.com/Expresso.htm
and get a copy of Expresso - it's free, and it examines and generates Regular expressions.
CPallini 17-Dec-15 3:58am    
5.
Attention members of the RegEx cult :) ... This solution is presented only as an example; no claim is made that this is any better coding technique than if one used RegEx. No claim is made that this is optimal code (performs faster, uses less memory) for a coded solution to this kind of parsing scenario. What this humble flea does claim, however, is that:

a. using a given coding problem to create small tools that you'll be likely to re-use in the future is a good idea.

b. for some people, like this humble flea, who is not such a RegEx expert, it is possible that code where the solution is inherently "visible," may be easier to maintain/modify ... for yourself, and others ... in the future.

Finally, lest ye judge, may I propose that seeing an example in code that illustrates what RegEx can save you from coding may be useful to newcomers.

So, in the case you might find it interesting to see how such validation is handled in code, rather than with RegEx:

a. I'm always looking to build small tools when coding that can be re-used elsewhere (often putting them, later, as static extension methods, into a library .dll containing similar functions):
C#
// all code shown here requires:
using System.Linq;
using System.Collections.Generic;

private const int MinYears = 16;
private const int MaxYears = 90;

private static bool AllAlpha(string test)
{
    return ! test.Any(ch => ! Char.IsLetter(ch));
}

private static bool AllNumeric(string test)
{
    return ! test.Any(ch => ! Char.IsDigit(ch));
}

private static bool IsYear(string year)
{
    DateTime birthyeardate;

    if(DateTime.TryParse(@"1/1/" + year, out birthyeardate))
    {
        // perform additional date validation here

        int yearsold = DateTime.Now.Year - birthyeardate.Year;

        if (yearsold >= MinYears && yearsold <= MaxYears) return true;
    }

    return false;
}
b. then, I might "sketch out" a method I'd like to use for implementation:
C#
public bool TestStringSegments(string source, List<func><string,>> functions)
{
    foreach (var func in functions)
    {
        if (!func(source)) return false;
    }

    return true;
}

// a sample list of functions

//1.First 2 alphabets.
//2.Next 2 numeric.
//3.Next 4 a valid year.
//4.Remaining 7 numeric.

List<func><string,>> DLValidateTests = new List<func><string,>>
{
    str => AllAlpha(str.Substring(0,2)),
    str => AllNumeric(str.Substring(2,2)),
    str => IsYear(str.Substring(4,4)),
    str => AllNumeric(str.Substring(8)) 
}; </func></func></func>
The idea here is to execute each Function in a List of Functions passed in as a parameter, where each Function in the List returns a boolean, and to return 'true only if all Functions return 'true. The 'foreach code is written in such a way that this function returns immediately when any "in the list" function it executes returns 'false.

Here's how this might be used in your case:
C#
private void SomeButton_Click(object sender, EventArgs e)
{
    // good to go
    bool validated1 = TestStringSegments("AB2219991234567", DLValidateTests);
    // alpha char in date error
    bool validated2 = TestStringSegments("AB2220x51234567", DLValidateTests);
    // date out of range
    bool validated3 = TestStringSegments("AB2219191234567", DLValidateTests);
    // number in alpha
    bool validated4 = TestStringSegments("A32219981234567", DLValidateTests);

    // put a break-point here, inspect 'validated#x values
}
 
Share this answer
 
v3
Comments
Mitul Birla 17-Dec-15 5:58am    
Thanks BillWoodruff!!!
Great one!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900