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:
When I run the program as is I get this output:
123456789
No 'YES' for a match.
Thanks for any help.
Dale
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:
Code:
##########################################################
#!/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