Posted By

Shifter on 07/12/09


Tagged

number credit card validate


Versions (?)

Who likes this?

5 people have marked this snippet as a favorite

luman
vali29
irishsk
DesignByWilliam
Goddard


Validate Credit Card Number


 / Published in: PHP
 

  1. function fnValidateCc($mValue, $sCard = '')
  2. {
  3. if ($sCard == 'visa')
  4. {
  5. $sPattern = '/^4([0-9]{12}|[0-9]{15})$/';
  6. }
  7. else if ($sCard == 'amex')
  8. {
  9. $sPattern = '/^3[4|7][0-9]{13}$/';
  10. }
  11. else if ($sCard == 'mastercard')
  12. {
  13. $sPattern = '/^5[1-5][0-9]{14}$/';
  14. }
  15. else if ($sCard == 'discover')
  16. {
  17. $sPattern = '/^6011[0-9]{12}$/';
  18. }
  19. else if ($sCard == 'dinners')
  20. {
  21. $sPattern = '/^[30|36|38]{2}[0-9]{12}$/';
  22. }
  23. else if (empty($sCard))
  24. {
  25. $sPattern = '/^[0-9]{13,19}$/';
  26. }
  27. else
  28. {
  29. return false;
  30. }
  31.  
  32. if (preg_match($sPattern, $mValue))
  33. {
  34. // Modulo 10
  35. $iSum = 0;
  36. $iWeight = 2;
  37. $iLength = strlen($mValue);
  38.  
  39. for ($i = $iLength - 2; $i >= 0; $i--)
  40. {
  41. $iDigit = $iWeight * $mValue[$i];
  42. $iSum += floor($iDigit / 10) + $iDigit % 10;
  43. $iWeight = $iWeight % 2 + 1;
  44. }
  45.  
  46. if ((10 - $iSum % 10) % 10 == $mValue[$iLength - 1])
  47. {
  48. return true;
  49. }
  50. }
  51.  
  52. return false;
  53. }

Report this snippet  

You need to login to post a comment.