Return to Snippet

Revision: 15633
at July 12, 2009 12:18 by Shifter


Updated Code
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;
}

Revision: 15632
at July 12, 2009 12:13 by Shifter


Updated Code
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 == 'master')
  {
    $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;
}

Revision: 15631
at July 12, 2009 12:13 by Shifter


Initial Code
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 == 'master')
  {
    $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;

}

Initial URL


Initial Description


Initial Title
Validate Credit Card Number

Initial Tags
number

Initial Language
PHP