Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 255505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:56:11+00:00 2026-05-11T21:56:11+00:00

I’m trying to validate a drivers license for a form that i am making.

  • 0

I’m trying to validate a drivers license for a form that i am making.
I was trying to use a single regex.

  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

I’m new at regex I’m googling trying to work it out. Help with this would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T21:56:11+00:00Added an answer on May 11, 2026 at 9:56 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I need to clean up various Word 'smart' characters in user input, including but
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.