Looking for a certain regexp

Discussion in 'Perl Misc' started by Robert TV, Jun 30, 2004.

  1. Robert TV

    Robert TV Guest

    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
     
    Robert TV, Jun 30, 2004
    #1
    1. Advertising

  2. Robert TV wrote:
    > 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
     
    Jürgen Exner, Jun 30, 2004
    #2
    1. Advertising

  3. Robert TV

    Robert TV Guest

    "Robert TV" <> wrote:

    > 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
     
    Robert TV, Jun 30, 2004
    #3
  4. Robert TV

    Robert TV Guest

    "Jürgen Exner" <> wrote:

    > 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
     
    Robert TV, Jun 30, 2004
    #4
  5. Robert TV wrote:
    > Jürgen Exner wrote:
    >> Why aren't you simply using the Business::CreditCard module?

    >
    > I do not own my own server


    Okay.

    > or have the ability to install such modules that are not bundled
    > with Perl5.


    Yes, you have.

    > 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 : (


    Have you asked? If they say no, then do it yourself.

    perldoc -q "own module"

    http://www.mail-archive.com//msg59382.html

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 30, 2004
    #5
  6. "Robert TV" <> writes:
    > 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.

    > however


    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
    --
    Come to think of it, there are already a million monkeys on a million
    typewriters, and Usenet is NOTHING like Shakespeare.
    -- Blair Houghton.
     
    Eric Schwartz, Jun 30, 2004
    #6
  7. Robert TV

    Daedalus Guest

    > 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
     
    Daedalus, Jun 30, 2004
    #7
  8. Robert TV wrote:
    > "Jürgen Exner" <> wrote:
    >
    >> 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.


    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
     
    Jürgen Exner, Jun 30, 2004
    #8
  9. Robert TV

    Robert TV Guest

    "Gunnar Hjalmarsson" <> wrote

    > Have you asked? If they say no, then do it yourself.
    >
    > perldoc -q "own module"
    >
    > http://www.mail-archive.com//msg59382.html


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

    Robert
     
    Robert TV, Jun 30, 2004
    #9
  10. Robert TV

    Robert TV Guest

    "Eric Schwartz" <> wrote

    > 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
     
    Robert TV, Jun 30, 2004
    #10
  11. Robert TV

    Robert TV Guest

    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
     
    Robert TV, Jun 30, 2004
    #11
  12. 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.

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jun 30, 2004
    #12
  13. Robert TV wrote:

    > I know very little about CPAN but am starting to learn its a good
    > resource.


    Did you know you can search module names & descriptions at
    <http://search.cpan.org>? Very handy.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jun 30, 2004
    #13
  14. Robert TV wrote:
    > Gunnar Hjalmarsson wrote:
    >> Have you asked? If they say no, then do it yourself.
    >>
    >> perldoc -q "own module"
    >>
    >> http://www.mail-archive.com//msg59382.html

    >
    > 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. :(

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 30, 2004
    #14
  15. Robert TV

    John Bokma Guest

    Abigail wrote:

    > Sherm Pendley () wrote on MMMCMLVI September MCMXCIII
    > in <URL:news:p>:
    > `` 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>
    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jun 30, 2004
    #15
  16. Abigail wrote:

    > 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--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, Jun 30, 2004
    #16
  17. >>>>> "PG" == Purl Gurl <> writes:

    PG> Abigail wrote:
    >> Checksums *can* be checked with a regexp.


    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.

    >> Now, if only I could find the URLs with the checksum formulas


    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

    --
    Dale Henderson

    "Imaginary universes are so much more beautiful than this stupidly-
    constructed 'real' one..." -- G. H. Hardy
     
    Dale Henderson, Jun 30, 2004
    #17
  18. Robert TV

    John Bokma Guest

    John Bokma, Jun 30, 2004
    #18
  19. >>>>> "JB" == John Bokma <> writes:

    JB> Dale Henderson wrote:
    >> How dare you harass Abigail just because she is female. :)


    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. :)

    --
    Dale Henderson

    "Imaginary universes are so much more beautiful than this stupidly-
    constructed 'real' one..." -- G. H. Hardy
     
    Dale Henderson, Jul 1, 2004
    #19
  20. >>>>> "PG" == Purl Gurl <> writes:

    PG> Dale Henderson wrote:

    PG> Your news reader is broken.

    There is nothing wrong with my news reader.


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


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


    PG> Essentially? Classic MD5 checksum, yes?

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

    >> > 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.


    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).


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



    PG> Ha. Ha. Funny boy.

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

    PG> Purl Gurl

    "Happy trails to you..."

    --
    Dale Henderson

    "Imaginary universes are so much more beautiful than this stupidly-
    constructed 'real' one..." -- G. H. Hardy
     
    Dale Henderson, Jul 1, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward
    Replies:
    6
    Views:
    504
    Edward
    Sep 1, 2004
  2. Replies:
    0
    Views:
    673
  3. Replies:
    2
    Views:
    545
    bruce barker
    Mar 25, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    391
    7stud --
    Aug 21, 2009
  5. SAN CAZIANO
    Replies:
    8
    Views:
    193
    Dr John Stockton
    Oct 15, 2004
Loading...

Share This Page