# Posted By

Shifter on 07/12/09

# Statistics

Viewed 626 times
Favorited by 3 user(s)

# Validate Credit Card Number

/ Published in: PHP
`function fnValidateCc(\$mValue, \$sCard = ''){  if (\$sCard == 'visa')  {    \$sPattern = '/^4([0-9]{12}|[0-9]{15})\$/';  }  else if (\$sCard == 'amex')  {    \$sPattern = '/^3[4|7][0-9]{13}\$/';  }  else if (\$sCard == 'mastercard')  {    \$sPattern = '/^5[1-5][0-9]{14}\$/';  }  else if (\$sCard == 'discover')  {    \$sPattern = '/^6011[0-9]{12}\$/';  }  else if (\$sCard == 'dinners')  {    \$sPattern = '/^[30|36|38]{2}[0-9]{12}\$/';  }  else if (empty(\$sCard))  {    \$sPattern = '/^[0-9]{13,19}\$/';  }  else  {    return false;  }   if (preg_match(\$sPattern, \$mValue))  {    // Modulo 10     \$iSum = 0;    \$iWeight = 2;    \$iLength = strlen(\$mValue);     for (\$i = \$iLength - 2; \$i >= 0; \$i--)     {      \$iDigit = \$iWeight * \$mValue[\$i];      \$iSum += floor(\$iDigit / 10) + \$iDigit % 10;      \$iWeight = \$iWeight % 2 + 1;    }     if ((10 - \$iSum % 10) % 10 == \$mValue[\$iLength - 1])     {      return true;    }  }   return false;}`