You are here

public static function YamlFormCreditCardNumber::validCreditCardNumber in YAML Form 8

Validation rule for credit card number.

Luhn algorithm number checker - (c) 2005-2008 shaman - www.planzero.org This code has been released into the public domain, however please give credit to the original author where possible.

@see: http://stackoverflow.com/questions/174730/what-is-the-best-way-to-valida...

Parameters

string $number: A credit card number.

Return value

bool TRUE is credit card number is valid.

1 call to YamlFormCreditCardNumber::validCreditCardNumber()
YamlFormCreditCardNumber::validateYamlFormCreditCardNumber in src/Element/YamlFormCreditCardNumber.php
Form element validation handler for #type 'creditcard_number'.

File

src/Element/YamlFormCreditCardNumber.php, line 74

Class

YamlFormCreditCardNumber
Provides a form element for entering a credit card number.

Namespace

Drupal\yamlform\Element

Code

public static function validCreditCardNumber($number) {

  // If number is not 15 or 16 digits return FALSE.
  if (!preg_match('/^\\d{15,16}$/', $number)) {
    return FALSE;
  }

  // Set the string length and parity.
  $number_length = strlen($number);
  $parity = $number_length % 2;

  // Loop through each digit and do the maths.
  $total = 0;
  for ($i = 0; $i < $number_length; $i++) {
    $digit = $number[$i];

    // Multiply alternate digits by two.
    if ($i % 2 == $parity) {
      $digit *= 2;

      // If the sum is two digits, add them together (in effect).
      if ($digit > 9) {
        $digit -= 9;
      }
    }

    // Total up the digits.
    $total += $digit;
  }

  // If the total mod 10 equals 0, the number is valid.
  return $total % 10 == 0 ? TRUE : FALSE;
}