Credit card validation script in PHP

I have created a simple php function to validate credit card number using regex function, In this function you need to pass two argument card type and card number, After passing these two argument function will return True Or False depending on whether the credit card number combination is found to be valid.
cvalid
Here is the validation function you can add more cards validation as per your need, Just create one more switch case inside this function.

<?php 
function CCValidate($type, $cNum) {
    switch ($type) {
    case "American":
        $pattern = "/^([34|37]{2})([0-9]{13})$/";//American Express
		return (preg_match($pattern,$cNum)) ? true : false; 
        break;
    case "Dinners":
        $pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
		return (preg_match($pattern,$cNum)) ? true : false;
        break;
    case "Discover":
        $pattern = "/^([6011]{4})([0-9]{12})$/";//Discover Card
		return (preg_match($pattern,$cNum)) ? true : false;
        break;
    case "Master":
        $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";//Mastercard
		return (preg_match($pattern,$cNum)) ? true : false;
        break;
    case "Visa":
        $pattern = "/^([4]{1})([0-9]{12,15})$/";//Visa
		return (preg_match($pattern,$cNum)) ? true : false; 
        break;               
   }
} 
?>




Created simple demo html page to validate credit card number.

<form action="" method="post">
<select name="type">
<option value="American">American Express</option>
<option value="Dinners">Diner's Club</option>
<option value="Discover">Discover</option>
<option value="Master">Master Card</option>
<option value="Visa">Visa</option>
</select>
<input type="text" name="cNum">
<button type="submit">Submit</button>
</form>

Now call your CCValidation function on whatever event you want to perform validation action, In my case i used after form submit. And set error or success message.

<?php
  if(isset($_REQUEST) && !empty($_REQUEST)) {
  	echo (CCValidate($_REQUEST['type'], $_REQUEST['cNum'])) ? "<h3>Credit Card Valid.</h3>" : "<h3>Credit Card Invalid, Please check again..!!</h3>";
  }
?>

So Now your complete index.php file will be.

index.php

<?php 
function CCValidate($type, $cNum) {
    switch ($type) {
    case "American":
        $pattern = "/^([34|37]{2})([0-9]{13})$/";//American Express
		return (preg_match($pattern,$cNum)) ? true : false; 
        break;
    case "Dinners":
        $pattern = "/^([30|36|38]{2})([0-9]{12})$/";//Diner's Club
		return (preg_match($pattern,$cNum)) ? true : false;
        break;
    case "Discover":
        $pattern = "/^([6011]{4})([0-9]{12})$/";//Discover Card
		return (preg_match($pattern,$cNum)) ? true : false;
        break;
    case "Master":
        $pattern = "/^([51|52|53|54|55]{2})([0-9]{14})$/";//Mastercard
		return (preg_match($pattern,$cNum)) ? true : false;
        break;
    case "Visa":
        $pattern = "/^([4]{1})([0-9]{12,15})$/";//Visa
		return (preg_match($pattern,$cNum)) ? true : false; 
        break;               
   }
} 
?>
 
<?php
  if(isset($_REQUEST) && !empty($_REQUEST)) {
  	echo (CCValidate($_REQUEST['type'], $_REQUEST['cNum'])) ? "<h3>Credit Card Valid.</h3>" : "<h3>Credit Card Invalid, Please check again..!!</h3>";
  }
?>
 
<form action="" method="post">
<select name="type">
<option value="American">American Express</option>
<option value="Dinners">Diner's Club</option>
<option value="Discover">Discover</option>
<option value="Master">Master Card</option>
<option value="Visa">Visa</option>
</select>
<input type="text" name="cNum">
<button type="submit">Submit</button>
</form>

DEMO DOWNLOAD

If you like this post please don’t forget to subscribe my public note book for more useful stuff

Posted in: PHP

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.