Dynadot โ€” .com Registration $8.99

Data Validation script for forms

Spacemail by SpaceshipSpacemail by Spaceship
Watch
Impact
0
I have built myself what I need from a basic html form but I need some a data validation script to run it, including credit card field validation.

Can someone point in the right direction? I have seen some older credit validation scripts on hotscripts but not sure they are current enough.

Thanks in advance.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
Unstoppable DomainsUnstoppable Domains
What scripting language?
 
0
•••
Krazza said:
I have seen some older credit validation scripts on hotscripts but not sure they are current enough.

This stuff doesn't change often. If it did, the banking industry would go nuts. That's why the scripts are older; there's nothing to update. You'll be fine with any of the more popular ones (as indicated by the red "chili pepper" markers).

If you're concerned about using something canned, you can google up the info on how credit card numbers are constructed and checksummed, and simply roll your own.
 
0
•••
As far as CC numbers..try..

PHP:
function check_cc_num($number, $name)
{
	$cards = array(
		array(
			'name'     => 'American Express',
			'length'   => '15',
			'prefixes' => '34,37',
		),
		array(
			'name'     => 'Diners Club',
			'length'   => '14',
			'prefixes' => '300,301,302,303,304,305,36,38'
		),
		array(
			'name'     => 'Discover',
			'length'   => '16',
			'prefixes' => '6011'
		),
		array(
			'name'     => 'MasterCard',
			'length'   => '16',
			'prefixes' => '51,52,53,54,55'
		),
		array(
			'name'     => 'Visa', 
			'length'   => '13,16', 
			'prefixes' => '4'
		)
	);

	// Remove any space
	$number = preg_replace('#\s#', '', $number);

	if (strlen($number) == 0)
	{
		return 'No card number provided.';
	}

	// Determine if we even support this card type..
	$type = -1;

	for ($i = 0; $i <= count($cards); $i++)
	{
		if (strtolower($name) == strtolower($cards[$i]['name']))
		{
			$type = $i;
			break;
		}
	}

	if ($type == -1)
	{
		return 'Unknown card type';
	}

	// Valid length? (lengths vary per credit card..
	if (!preg_match('#^[0-9]{13,19}$#', $number))
	{
		return 'Credit card number invalid.';
	}

	$checksum = 0;
	$j = 1;

	for ($i = strlen($number) - 1; $i >= 0; $i--)
	{
		$calc = $number{$i} * $j;

		if ($calc > 9)
		{
			$checksum++;
			$calc -= 10;
		}

		$checksum = $checksum + $calc;
		$j = ($j == 1) ? 2 : 1;
	}

	if ($checksum % 10 != 0)
	{
		return 'Credit card number format invalid.';
	}

	// Does it have the correct prefix?
	$valid_prefix = false;

	if (strpos($cards[$type]['prefixes'], ',') !== false)
	{
		$prefixes = explode(',', $cards[$type]['prefixes']);

		for ($i = 0; $i < count($prefixes); $i++)
		{
			if (preg_match("#^{$prefixes[$i]}#", $number))
			{
				$valid_prefix = true;
				break;
			}
		}
	}
	else
	{
		if (preg_match("#^{$cards[$type]['prefixes']}#", $number))
		{
			$valid_prefix = true;
		}
	}

	if (!$valid_prefix)
	{
		return 'Credit card number invalid.';
	}

	// Does it have the correct length?
	$valid_length = false;

	if (strpos($cards[$type]['length'], ',') !== false)
	{
		$lengths = explode(',', $cards[$type]['length']);

		for ($i = 0; $i < count($lengths); $i++)
		{
			if (strlen($number) == $lengths[$i])
			{
				$valid_length = true;
				break;
			}
		}
	}
	else
	{
		if (strlen($number) == $cards[$type]['length'])
		{
			$valid_length = true;
		}
	}

	if (!$valid_length)
	{
		return 'Credit card number invalid.';
	}

	// If we make it this far, everything is OK
	return true;
}
 
Last edited:
0
•••
Unstoppable Domains
Domain Recover
DomainEasy โ€” Zero Commission
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back