You are here

function _brazilianids_validate_cpf in Brazilian IDs 6

Check if the provided CPF is valid

Parameters

&$cpf: Number, the CPF number

$clean: Boolean, FALSE to not clear the number

Return value

Boolean, true if its a valid CPF

3 calls to _brazilianids_validate_cpf()
brazilianids_field in ./brazilianids_cck.inc
Implementation of hook_field().
brazilianids_user in ./brazilianids.module
Implementation of hook_user().
_brazilianids_is_cpf_cnpj in ./brazilianids.check.inc
Return 'CPF' or 'CNPJ' depend of the type of param

File

./brazilianids.check.inc, line 172
brazilianids.check.inc

Code

function _brazilianids_validate_cpf(&$cpf, $clean = TRUE) {

  // Clear the CPF
  if ($clean) {
    $cpf = _brazilianids_clean_cpfcnpj($cpf);
  }

  // Check if its not the forbidden combinations
  if (drupal_strlen($cpf) != 11 or $cpf == '00000000000' or $cpf == '11111111111' or $cpf == '22222222222' or $cpf == '33333333333' or $cpf == '44444444444' or $cpf == '55555555555' or $cpf == '66666666666' or $cpf == '77777777777' or $cpf == '88888888888' or $cpf == '99999999999') {
    return NULL;
  }

  // Check the 11st and 12nd numbers
  for ($numbers = 9; $numbers <= 10; $numbers++) {
    $digit = 0;
    for ($i = 0; $i < $numbers; $i++) {
      $digit += $cpf[$i] * ($numbers + 1 - $i);
    }

    // Calculate the digit and check it
    $digit = 11 - $digit % 11;
    if ($digit == 10 or $digit == 11) {
      $digit = 0;
    }
    if ($digit != $cpf[$numbers]) {
      return NULL;
    }
  }
  return $cpf;
}