Credit Card Number Validation using PHP
anoopsachari | Apr 23, 2010 | Comments 1
I am sharing you a php function that would help you to validate credit card number . Its a ready made function thus can be used directly in any of your web projects .
php function
function is_valid_credit_card($s) {
$s = strrev(preg_replace('/[^\d]/','',$s));
$sum = 0;
for ($i = 0, $j = strlen($s); $i < $j; $i++) {
if (($i % 2) == 0) {
$val = $s[$i];
} else {
$val = $s[$i] * 2;
if ($val > 9) { $val -= 9; }
}
$sum += $val;
}
return (($sum % 10) == 0);
}if (! is_valid_credit_card($_POST['credit_card'])) {
print 'Sorry, that card number is invalid.';
}
Hope this one helped you !
Filed Under: php
About the Author: a holistic web developer , movie buff and technical blogger from queen of arabian sea.







Hi, I want include a shopping card in my portal, I can user your php?