Ashworth Consulting

Main navigation

  • Home
  • About Us
  • Solutions
    • Web Design
    • Search Engine Marketing
      • $99 SEO Report
      • Silver SEO Package
      • Gold SEO Package
      • Platinum SEO Package
    • Custom Solutions
  • Web Hosting
    • Shared Web Hosting
    • SEO Web Hosting
    • Enterprise Hosting
    • Terms of Service
  • Articles
  • Contact Us
    • Support
    • Submit Files to Us
    • Request A Quote
  • ← Happy New Year!
  • Network Maintenance on 2/8/2010 →

Validating a Credit Card

Posted on Jan 15, 2010 by Ashworth Consulting

This little function helps validating a given credit card number is legit:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
function ValidateCreditCardNumber($cc_num)
{
  $pattern = "/^3[47]\d{13}$/";//American Express
  if (preg_match($pattern,$cc_num))
  {
    return true;
  }
 
  $pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
  if (preg_match($pattern,$cc_num))
  {
    return true;
  }
 
  $pattern = "/^6011\d{12}$/";//Discover Card
  if (preg_match($pattern,$cc_num))
  {
    return true;
  }
 
  $pattern = "/^5[12345]\d{14}$/";//Mastercard
  if (preg_match($pattern,$cc_num))
  {
    return true;
  }
 
  $pattern = "/^4\d{12}(\d\d\d){0,1}$/";//Visa
  if (preg_match($pattern,$cc_num))
  {
    return true;
  }
 
  $pattern = "/^30[012345]\d{11}$/";//Diners
  if (preg_match($pattern,$cc_num))
  {
    return true;
  }
 
  $pattern = "/^3[68]\d{12}$/";//Diners #2
  if (preg_match($pattern,$cc_num))
  {
    return true;
  }
 
  // Not valid
  return false; 
}
?>

Category: PHP Tagged: code , credit card , PHP , validate

top ^

3 Responses to Validating a Credit Card

  1. Zach | February 3, 2010 at 12:42 am

    Excellent code man!

  2. admin | February 3, 2010 at 9:15 am

    Thanks. This one’s come in handy on more than one project. I hate pattern matching with a passion but it’s essential if you want to get down to the tricky stuff.

  3. Zach | February 8, 2010 at 4:39 am

    The code could probably be probably be more library-ized to say that:

    if (is_cc($_POST[‘cc’], CC_VISA)) {
    // sweet, we take that, and it’s valid
    do_visa_stuff($_POST[‘cc’]);
    }

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Categories

  • Featured Articles
  • General
  • Marketing
  • Operations & Server Status
  • PHP
  • Portfolio
  • Reviews
  • Scripts & Code
  • Security
  • SEO
  • Web Design
  • Web Hosting
  • WordPress

Contact Us

Ashworth Consulting
1506 NE Everett St
Camas WA 98607

Email
info@ashworthconsulting.com

Technical Support & Help

Shared hosting customers requiring technical support should email support@ashworthconsulting.com for assistance with their account.

Additional information can be found on our support page.

Useful Links

  • Client Support & Billing System
  • cPanel User Manual
  • Sitemap of this website

Legal Information

  • Privacy Policy
  • Terms of Service

Recent Articles

  • WP Super Cache security update issued. Fixes nasty XSS bug.
  • Updated CentOS kernel to 2.6.32-431.23.3.el6 on SAPPHIRE
  • ISP-Related Networking Issues
  • Service disruption to LW1 web server
Ashworth Consulting | © 2020 All rights reserved.
  • Request A Quote
  • Contact Us