function br_tax_number_cpf_validator in Brazilian IDs 7
Function to validate if value is a cpf
Parameters
string $value:
Return value
array TRUE or FALSE and MSG error
1 call to br_tax_number_cpf_validator()
- br_tax_number_fields_cpf_validate in ./
br_tax_number_fields.module - Validation callback.
File
- ./
br_tax_number_fields.module, line 220 - Adds Brazilian Tax Number field widgets to text field type at the Field UI and creates new form element types for use in the Form API.
Code
function br_tax_number_cpf_validator($value) {
if (!empty($value)) {
$forbidden = array(
'00000000000',
'11111111111',
'22222222222',
'33333333333',
'44444444444',
'55555555555',
'66666666666',
'77777777777',
'88888888888',
'99999999999',
);
if (in_array($value, $forbidden)) {
return array(
'status' => FALSE,
'msg' => t('CPF field does not allow a sequence of the same number.'),
);
}
if (strlen($value) != 11) {
return array(
'status' => FALSE,
'msg' => t('CPF must have 11 digits.'),
);
}
else {
// @TODO - Translate variables to english
$cpf_validar = substr($value, 0, 9);
$soma = 0;
$n = 11;
for ($i = 0; $i <= 9; $i++) {
$n = $n - 1;
$soma = $soma + substr($cpf_validar, $i, 1) * $n;
}
$resto = $soma % 11;
if ($resto < 2) {
$cpf_validar .= 0;
}
else {
$cpf_validar = $cpf_validar . (11 - $resto);
}
// Segunda parte da validação do CPF.
$soma = 0;
$n = 12;
for ($i = 0; $i <= 10; $i++) {
$n = $n - 1;
$soma = $soma + substr($cpf_validar, $i, 1) * $n;
}
$resto = $soma % 11;
if ($resto < 2) {
$cpf_validar .= 0;
}
else {
$cpf_validar = $cpf_validar . (11 - $resto);
}
if ($cpf_validar != $value) {
return array(
'status' => FALSE,
'msg' => t('CPF number you have entered is invalid.'),
);
}
}
}
return array(
'status' => TRUE,
);
}