Subscribe to Tutorial Feed

Flash and PHP Bible

The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.

The Flash and PHP Bible has a forum for quick support.

Scriptplayground » tutorials » php » Validating Credit Cards Using PHP

Validating Credit Cards Using PHP

Using PHP to validate major credit cards.

View an Example of this article before you get started.

In the following tutorial we will learn how to validate credit cards using PHP. Note this will only validate the card number is the correct format, but will NOT verify the card is active. Lets get started.

I am sure you are familiar with credit card forms on the web, whether an online store, bill pay service or donation, they are all over the place. While you can use Javascript (client side) scripting to validate visually its good to also validate in PHP (server side) to ensure proper submission.

We will pack all of this validation in a function for easy re-use.

Start by creating the basic function and defining the variables to be used.

function validateCC($card=null, $type=null)
{
	$error = null;
	$verified = false;
	$regexPattern = null;
}

Now lets verify the passed in arguments are in fact valid before continuing.

function validateCC($card=null, $type=null)
{	
	...
	
	if(is_null($card) || empty($card))
	{
		$error = "Credit card number is required.";
		return false;
	}
	
	if(is_null($type) || empty($type))
	{
		$error = "Credit card type (ex: visa) is required.";
		return false;
	}
}

Okay, now that we are sure a valid card and type have been passed in, lets choose the correct validation pattern. This pattern will be used to validate the card.

function validateCC($card=null, $type=null)
{	
	...
	
	if($type == "visa")
	{
		$regexPattern = "/^4[0-9]{12}(?:[0-9]{3})?$/";
	}
	else if($type == "discover")
	{
		$regexPattern = "/^6(?:011|5[0-9]{2})[0-9]{12}$/";
	}
	else if($type == "amex")
	{
		$regexPattern = "/^3[47][0-9]{13}$/";
	}
	else if($type == "mc")
	{
		$regexPattern = "/^5[1-5][0-9]{14}$/";
	}
	else if($type == "diners")
	{
		$regexPattern = "/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/";
	}
	else if($type == "jcb")
	{
		$regexPattern = "/^(?:2131|1800|35\d{3})\d{11}$/";
	}
	else
	{
		return false;
	}
}

Now based on the $regexPattern chosen based on the credit card type lets use preg_match to validate the card.

function validateCC($card=null, $type=null)
{	
	...
	
	if(preg_match($regexPattern, $card))
	{	
		$verified = true;
	}
	
	return $verified;
}

Thats all there is to validating a card is the correct format. I hope you enjoyed this tutorial. As always, if you have comments or questions, please post them below.

Here is the completed function for easy copying/pasting.

function validateCC($card=null, $type=null)
{	
	$error = null;
	$verified = false;
	$regexPattern = null;
	
	if(is_null($card) || empty($card))
	{
		$error = "Credit card number is required.";
		return false;
	}
	
	if(is_null($type) || empty($type))
	{
		$error = "Credit card type (ex: visa) is required.";
		return false;
	}
	
	// Valid args, lets validate card now
	
	if($type == "visa")
	{
		$regexPattern = "/^4[0-9]{12}(?:[0-9]{3})?$/";
	}
	else if($type == "discover")
	{
		$regexPattern = "/^6(?:011|5[0-9]{2})[0-9]{12}$/";
	}
	else if($type == "amex")
	{
		$regexPattern = "/^3[47][0-9]{13}$/";
	}
	else if($type == "mc")
	{
		$regexPattern = "/^5[1-5][0-9]{14}$/";
	}
	else if($type == "diners")
	{
		$regexPattern = "/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/";
	}
	else if($type == "jcb")
	{
		$regexPattern = "/^(?:2131|1800|35\d{3})\d{11}$/";
	}
	else
	{
		return false;
	}
	
	if(preg_match($regexPattern, $card))
	{	
		$verified = true;
	}
	
	return $verified;
}

Follow Scriptplayground on Twitter (@scriptplay)

Comments: Validating Credit Cards Using PHP

No Comments have been made. Wanna be the first?
Add a comment
The ability to add new comments has been disabled in this archived version.

Loading
Main | Tutorials | Articles