Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Pattern matching -- driver's license number validation

phinsman (Programmer) (OP)
8 Nov 03 2:10
Hello all...

I'm trying to validate driver's license numbers for each state.  I'm going to use a hash of patterns.  Some states, such as Arkansas, my example below, have more than one possible method.

I'm getting the pattern to match properly on all three possibilities.  However, I need to make sure that there isn't extra text at the beginning or ending of the user's input.  Otherwise, the validation is useless.  For example:

123-45-6789 is correct
123-45-6789extra_text is NOT correct.

I used the ^ and $ symbols.  The ^ works fine; extra text at the beginning is disallowed, and it matches properly if there isn't any.  The $ sign, however, isn't working for me.  No matter what I try I can't get it to match.  I have tried adding () around each possibility but that does not work either.

Code is below:


##########################################################
#!/usr/bin/perl

use strict;

#Use a hash for patterns
my %p = ();

#Valid patterns for Arkansas:
# 123456789     (SSN, no hyphens)
# 123-45-6789   (SSN with hyphens)
# 912345678     (9 then 8 digits)

$p{'AR'} = '^[\d]{3}[\d]{2}[\d]{3}$'.
           '|'.
           '^[\d]{3}\-[\d]{2}\-[\d]{3}$'.
           '|'.
           '^9[\d]{8}$';

#Sample driver license number
my $dl = '123456789';

#$match is just a variable that reports YES if a match if found
my $match = '';

#Check for a match
if($dl =~ /$p{'AR'}/) {
        $match = "YES";
}

#Output the driver license number, and whether it matched or not
my $line = sprintf("%-14s  %-4s",$dl,$match);
print $line,"\n";

#Exit the program
exit;
##########################################################


When I run the program as is I get this output:
123456789

No 'YES' for a match.

Thanks for any help.

Dale
siberian (Programmer)
8 Nov 03 3:01
All of your regex are matching on 8, 9 not digits


Change $dl to

my $dl = '12345678';


And it matches
--

^[\d]{3}[\d]{2}[\d]{3}$
# 3 + 2 + 3 == 8
^[\d]{3}\-[\d]{2}\-[\d]{3}$
3 + 2 + 3 == 8
^9[\d]{8}$
# 8 == 8
dchoulette (Programmer)
8 Nov 03 6:10
I tried to compact it a little:

#!/usr/bin/perl

use strict;

#Use a hash for patterns
my %p = (
     AR => '^(9\d{8}|\d{3}(-?)\d{2}\2\d{3})$'
     );

foreach my $dl qw(
12345678
912345678
123-45-678
123-45678
12345-678
812345678
stuff123-45-678
123-45-678stuff
abc-de-fgh
) {
    my $valid = $dl =~ m/$p{'AR'}/;
    print "License number '$dl' is " . ($valid ? "valid" : "invalid") ."\n"
}

--------------------
"When all else has failed, read the manuals."

Denis

phinsman (Programmer) (OP)
8 Nov 03 7:41
Wow...all that work and my only mistake was that I couldn't count!

Thanks a lot for your help guys.  At least the problem is solved; I was truly frustrated with it but it's nice when it's something simple that doesn't really need fixing.

Thanks again!

Dale

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close