Looking for a certain regexp

From: Robert TV (ducott_at_hotmail.com)
Date: 06/30/04


Date: Tue, 29 Jun 2004 23:16:33 GMT

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



Relevant Pages

  • Re: Looking for a certain regexp
    ... None of your syntax will "validate" a credit card. ... match specific mathematical formulas, and produce ...
    (comp.lang.perl.misc)
  • Re: Regular Expressions: How to check for these characters?
    ... >> These characters are not allowed in file names, that is why I need to ... >> validate if file names dont contain these characters. ... >> ' Validates if certain characters exist inside a string expression. ... >> Dim oReg As New RegExp ...
    (microsoft.public.vb.general.discussion)
  • Re: Data Validation: two conditions
    ... The op wants the last 3 characters to be ###. ... >> Can I do this with data validation? ... >validate the two components? ... >> MVP Microsoft Word and Word for Mac ...
    (microsoft.public.excel.worksheet.functions)
  • RE: Post Code UK Structure Verification
    ... Dim invalid As Boolean ... ' Validate inner code ..... ... Notepad is used because copying from ... Sort to bring to the top records with too many characters ...
    (microsoft.public.excel.programming)
  • Re: Why do these characters display incorrectly?
    ... > characters that the W3C validator is picking up on! ... I smell a utf-8 BOM ... > How can I get these pages to validate? ... If you want it to be utf-8, fix your web server configuration. ...
    (uk.net.web.authoring)