Looking for a certain regexp

R

Robert TV

Hi, has onyone ever created a regexp to match common credit card formulas? I
have been asked to "validate" some CC#'s we have on file. I do not know the
formulas by which credit cards are generated. All I know is Visa always
starts with 4, Mastercard with 5, and Amex with 3 I think. It would be nice
to have:

$cardtype = param('cardtype');
$cardnumber = param('cardnumber');

if ($cardtype eq "Visa") {
unless ($cardnumber =~ /some valid visa formula/) {
print "It's Good";
} else {
print "It's Bad";
}
}

And so forth. You can find e-commerce websites all over the place that do a
basic check but I don't know the formulas used. At the very least, I would
like to validate the first number as being 4,5,3 etc. and that the scalar is
at least 13 characters long. I do not know how to match just the very first
char of a scalar ... can some one help me with this? EG:

$cardtype = param('cardtype');
$cardnumber = param('cardnumber');

if ($cardtype eq "Visa") {
unless ($cardnumber =~ /first character is 4/ && $cardnumber =~ /at least
13 characters long/) {
print "It's Good";
} else {
print "It's Bad";
}
}

I am very new to writing regexp's and my present knowledge is limited to
validating characters used, not their positions. This was my best guess to
match 4 as first (untested):

unless ($cardnumber =~ /4.*/) { #does not match character length as 13
however
print "It's Good";

} else {
print "It's Bad";
}

Thank you all.
Robert
 
J

Jürgen Exner

Robert said:
Hi, has onyone ever created a regexp to match common credit card
formulas? I have been asked to "validate" some CC#'s we have on file.
I do not know the formulas by which credit cards are generated.

Why aren't you simply using the Business::CreditCard module?

jue
 
R

Robert TV

Robert TV said:
unless ($cardnumber =~ /4.*/) { #does not match character length as 13
however
print "It's Good";

} else {
print "It's Bad";
}

I just wrote this regexp that seems to correctly match the first number as
4, although i'm not certain if it's the best way:

#!/usr/bin/perl

use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);

$thenumber = "46574656";

if ($thenumber =~ /^[4].*$/) {
print "It's Good";
} else {
print "It's Bad";
}
exit;

Now if I could only figure out how to match against a certain number if
characters (13) in the scalar. Your suggestions are appreciated.

Robert
 
R

Robert TV

Jürgen Exner said:
Why aren't you simply using the Business::CreditCard module?

I do not own my own server or have the ability to install such modules that
are not bundled with Perl5. I have an account with a commercial web hosting
company and a cgi-bin to run my scripts. They will not install custom
modules for me : (

Robert
 
E

Eric Schwartz

Robert TV said:
Hi, has onyone ever created a regexp to match common credit card formulas?

If you were to search for "Credit Card" on http://search.cpan.org/
you'd find Business::CreditCard which claims to

Validate/generate credit card checksums/names

You know, it's kinda rude to ask other people to search for you, when
you can just do the same thing yourself.
I have been asked to "validate" some CC#'s we have on file.

Do you know what "validate" means? Are you supposed to make sure
they're real credit card numbers, or just that they might be?
Business::CreditCard doesn't really validate cards; it just makes sure
that the credit card number is vaguely plausible.
I am very new to writing regexp's and my present knowledge is limited to
validating characters used, not their positions.

You should read 'perldoc perlretut', then. 'perldoc perlrequick' if
you're in a hurry.
This was my best guess to
match 4 as first (untested):

Why? You can test it yourself, easily.
unless ($cardnumber =~ /4.*/) { #does not match character length as 13

That will match a string with a literal '4' in it anywhere.

That will cause a syntax error.
print "It's Good";

} else {
print "It's Bad";
}

Sounds like you've got an idea that regexes can do anything; they can
do a lot, but they're not ideal for everything. Also, sometimes it's
clearer to express yourself not using a regex. For instance, if what
you're concerned with is to find the length of a string, use the
length() function ('perldoc -f length' to find out more).

-=Eric
 
D

Daedalus

I just wrote this regexp that seems to correctly match the first number as
4, although i'm not certain if it's the best way:

#!/usr/bin/perl

use CGI qw:)standard);
use CGI::Carp qw(fatalsToBrowser);

$thenumber = "46574656";

if ($thenumber =~ /^[4].*$/) {
print "It's Good";
} else {
print "It's Bad";
}
exit;

Now if I could only figure out how to match against a certain number if
characters (13) in the scalar. Your suggestions are appreciated.
At the very least, I would
like to validate the first number as being 4,5,3 etc. and that the scalar is
at least 13 characters long. I do not know how to match just the very first
char of a scalar ... can some one help me with this? EG:

Instead of the * quantifier, you could use {12,} who will match 12 or more
charaters (plus the leading 4 = 13).

/^4.{12,}/ #but that would match much more than digits

Using character classes might be the answer

/^4[\d]{12,}/ # should match a string that begin with 4 plus 12 digit

DAE
 
J

Jürgen Exner

Robert said:
I do not own my own server or have the ability to install such
modules that are not bundled with Perl5.

Option 1: perldoc -q module: "How do I keep my own module/library
directory?"
Option 2: instead of reinventing the wheel just copy the (relevant subset
of) code from that module into your own program

jue
 
R

Robert TV

Eric Schwartz said:
You know, it's kinda rude to ask other people to search for you, when
you can just do the same thing yourself.

I didn't want people to do my work for me, I really didn't know where to
start with my research. I know very little about CPAN but am starting to
learn its a good resource.
Do you know what "validate" means? Are you supposed to make sure
they're real credit card numbers, or just that they might be?
Business::CreditCard doesn't really validate cards; it just makes sure
that the credit card number is vaguely plausible.

I now have CPAN's CC validity module working.
You should read 'perldoc perlretut', then. 'perldoc perlrequick' if
you're in a hurry.

I'll have a look ty.
R
 
R

Robert TV

Thanx everyone, with your assistance I was able to install the CredCard.pm
module on the server and it's validating correctly with Gunnars tutorial on
maunual installations. I had to do a little bit of playing with the code to
get the validation to work inside an "if" condition, but it seems to work
now. Thanx again.

R
 
S

Sherm Pendley

Robert said:
Hi, has onyone ever created a regexp to match common credit card formulas?

No, because it's not something you can check with a regexp, beyond the bare
minimum of verifying that it's 16 numeric characters, and (as you've found)
that the first character corresponds to one of the CC types you accept.
I have been asked to "validate" some CC#'s we have on file.

The validation recipe is based in part on a checksumming algorithm, but even
that isn't a guarantee. All that will ensure is that a given number *could*
belong to a valid card, at some time in the past, present, or future. Even
if the number passes the checksum validation, it could belong to a card
that has been cancelled, overdrawn, expired, or one that has not yet been
issued to anyone.

There are also numbers used for testing purposes that pass checksumming, but
only pass muster with a bank when your merchant account is in a special
"testing" mode. Those exist so e-commerce apps can be tested without
racking up a bunch of charges on a valid card.

As others have mentioned, there's a CPAN module that will help with the
checksumming. For the rest, you'll have to establish a merchant account
with a bank that provides an API that you can use. (No, I don't know which
banks do... I haven't done e-commerce in many years. Anyone remember
Cybercash?)

sherm--
 
G

Gunnar Hjalmarsson

Robert said:
Thank you Gunnar, that tutorial helped me install a .pm on the
server and it works great.

You are welcome. It's a good option for people who run CGI scripts on
hosting accounts without shell access.

It's a pity that the above answer in the Perl FAQ does not mention the
'manual' variant. It leads to people being unnecessarily shut out from
using non-standard CPAN modules, and in effect stuck with Perl 4 style
scripts. :(
 
J

John Bokma

Abigail said:
Sherm Pendley ([email protected]) wrote on MMMCMLVI September MCMXCIII
in <URL:`` Robert TV wrote:
``
`` > Hi, has onyone ever created a regexp to match common credit card formulas?
``
`` No, because it's not something you can check with a regexp, beyond the bare
`` minimum of verifying that it's 16 numeric characters, and (as you've found)
`` that the first character corresponds to one of the CC types you accept.

Checksums *can* be checked with a regexp. Regexes for certain credit cards
are on my todo list for Regexp::Common. Now, if only I could find the
URLs with the checksum formulas....

http://www.webopedia.com/TERM/L/Luhn_formula.html
<http://www.google.com/search?q=credit+card+check+formula>
 
S

Sherm Pendley

Abigail said:
Checksums *can* be checked with a regexp.

Didn't know that, thanks.
Regexes for certain credit cards are on my todo list for Regexp::Common.
Now, if only I could find the URLs with the checksum formulas....

In addition to the links John provided, you could have a look at the code in
Business::CreditCard. Dunno if it uses regexes or not.

sherm--
 
D

Dale Henderson

PG> No, they cannot and returns are not a checksum, both within a
PG> context of credit card number validation.

PG> Returns are based on a mathematical formula which should
PG> return zero for modulus ten if a valid number.


The Luhn or "mod 10" formula is essentially a checksum.
Is just that you know the sum ahead of time i.e. 0.


PG> You cannot use a regex for validating credit card numbers.

I wouldn't be so quick to say what can't be done with a regular
expression. Regular expressions (especially Perl regular
expressions) are very powerful and can even be
used to check numbers for primality tests and even solve simple
Diophantine equations.

For example to test for primality use:

$p=7; #number to test for primality

$str="1"x$p;
if(str=~/^(11+)\1+$/){
print "$p is Composite\n";
}else{
print "$p is Prime\n"
}

Not an efficient test but it works none the less.

In fact the first step of the Luhn test can be done with:

$cc=1234567890123456

$cc=~s/(\d)(\d)/(x==9?9:($1*2)%9).$2/eg;

This assumes the length of $cc is even (for odd length a
different replacement is needed) and doubles the odd numbered
digits with appropriate modifications (add the digits of 2 digit
numbers).

For the next step putting a plus between the digits and using an
eval would work.

PG> Too bad you have me killfiled. You could learn from me, just
PG> as I learn from others. I have almost a decade of experience
PG> in working with credit card validation.

PG> http://www.beachnet.com/~hstiles/cardtype.html

PG> Others will learn from what I share. You will not. In time,
PG> others will know more than you, just as I know more about this
PG> topic than you. It is illogical to limit your ability to gain
PG> knowledge based upon personal bigotry.

PG> Fix your quoting style; it is as bad as Uri's and Randal's and
PG> a real chore to repair. Wastes bandwidth to boot.

How dare you harass Abigail just because she is female. :)

PG> Purl Gurl
 
D

Dale Henderson

JB> And her family, don't forget her family :-D

Oh you're right that single post attacked Abigail AND the groom
pictured on Abigail's web page. :)
 
D

Dale Henderson

PG> Dale Henderson wrote:

PG> Your news reader is broken.

There is nothing wrong with my news reader.


PG> Essentially? Classic MD5 checksum, yes?

You're right it is a checksum. (What does MD5 have to do with anything).

PG> So write us a regex which will run the Mod 10 formula.


I didn't say it *could* be done just that you should be careful
before declaring something impossible. I did post examples of
things that can be done with a regex that are not obvious.

The burden is on you to prove it *can't* be done. (Or on Abigail
to prove it can be, a challenge I'll bet he's up to).



PG> Ha. Ha. Funny boy.

What makes you think I'm a "boy". "Dale" is quite androgynous.

PG> Purl Gurl

"Happy trails to you..."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top