How to validate CEI in ruby?
To validate CPF and CNPJ I use 'brcpfcnpj' in Gemfile.
But I found nothing to validate the CEI.
How to validate the specific INSS registration in Ruby on Rails ?
5 answers
An explanation of the ERC validation can be found here:
Format:
EE.NNN.NNNNN/AD
, Where:
EE
- NumberNNNNNNNN
- NumberA
- ActivityD
- Check Digit(a) multiply the last 11 digits by their respective weights, as follows:
Weights:
7,4,1,8,5,2,1,6,3,7,4
Digits:EENNNNNNNNA
Calculation:
7 * E = X1 4 * E = X2 1 * N = X3 8 * N = X4 5 * N = X5 2 * N = X6 1 * N = X7 6 * N = X8 3 * N = X9 7 * N = X10 4 * A = X11 D (posição do dígito)
(B) add all products obtained under item "A"
Soma = X1+2+X3+X4+X5+X6+X7+X8+X9+X10+X11
(C) with the total obtained in item "b", add the Digit of the unit to the Digit of the ten.
Total = Dezena de soma + Unidade de soma
(D) subtract from 10 the Digit of the unit obtained in item "c".
Resultado = 10 - Unidade de Total
The Digit of the unit of the subtraction result will be the checking digit.
Digito verificador = Unidade de Resultado
A implementation of this written in Ruby:
def cei_valid?(cei_str)
cei = cei_str.gsub(/\D+/, "").chars.map(&:to_i)
return false unless cei.size == 12
sum = [7,4,1,8,5,2,1,6,3,7,4].zip(cei).map{|p|p.reduce(:*)}.reduce(:+)
dv = (10 - (sum%10 + sum/10)).abs % 10
return dv == cei.last
end
Example:
cei_valid? "11.583.00249/85"
# => true
So here goes:
def validaCEI(num)
num = num.gsub(/\D+/,'') #só digitos
return false if num.size!=12
x = []
numDV = num[-1].to_i
num = num.chop #remove digito verificador
[7,4,1,8,5,2,1,6,3,7,4].each_with_index { |peso,i| x << (peso * num[i].to_i) }
soma = 0
x.each {|n| soma += n }
dv1 = soma.to_s[-2].to_i + soma.to_s[-1].to_i
dv2 = 10 - dv1
(dv2.abs==numDV) ? true : false
end
Good afternoon, after spending a lot of time searching (unsuccessfully) for a function that validates the CEI (INSS specific registration) via javascript, I found the explanation of how to validate here and created a javascript function to validate. With me it worked right, follow the code for those who need it in the future.
/*
DATA: 08/05/2017
Valida o CEI digitado.
Faz um cálculo matemático de acordo com o peso: 7,4,1,8,5,2,1,6,3,7,4.
@PARAM: @Obj = OBJ DO CAMPO.
*/
function validarCEI(Obj) {
//Retira qualquer tipo de mascara e deixa apenas números.
var cei = Obj.value.replace(/[^\d]+/g, '');
if (cei == ""){
return false;
}
if (cei.length != 12) {
alert("CEI digitado é inválido!") ;
return false;
}
var peso = "74185216374";
var soma = 0;
//Faz um for para multiplicar os números do CEI digitado pelos números do peso.
//E somar o total de cada número multiplicado.
for (i = 1; i < 12; i++){
var fator = peso.substring(i - 1, i);
var valor = cei.substring(i - 1, i);
soma += (fator * valor);
}
//Pega o length do resultado da soma e desconta 2 para pegar somente a dezena.
var len = soma.toString().length - 2;
//pega a dezena
var dezena = soma.toString().substring(len);
//pega o algarismo da dezena
var algdezena = dezena.toString().substring(0, 1);
//pega o algarismo da unidade
var unidade = parseInt(soma) - (parseInt((soma / 10)) * 10);
//soma o algarismo da dezena com o algarismo da unidade.
soma = parseInt(algdezena) + unidade;
//pega o dígito (último número) do cei digitado.
var digitoCEI = cei.substring(11);
var digitoEncontrado = 10 - soma;
if (digitoCEI != digitoEncontrado) {
alert("CEI digitado é inválido!") ;
return false;
} else {
return true;
}
}
Validation of the CEI (INSS specific register) in Java.
private boolean validaCei(String str_cei) {
Formato: EE.NNN.NNNNN/AD (12 dígitos)
Onde:
EE - Número
NNNNNNNN - Número
A - Atividade
D - Dígito Verificador
String peso = "74185216374";
Integer soma = 0;
if (str_cei.length() != 12)
return false;
//Faz um for para multiplicar os números do CEI digitado pelos números do peso.
//E somar o total de cada número multiplicado.
for (int i = 1; i < 12; i++){
String fator = peso.substring(i - 1, i);
String valor = str_cei.substring(i - 1, i);
soma += (Integer.parseInt(fator) * Integer.parseInt(valor));
}
//Pega o length do resultado da soma e desconta 2 para pegar somente a dezena.
Integer len = soma.toString().length() - 2;
//Pega a dezena
Integer dezena = Integer.parseInt(soma.toString().substring(len));
//Pega o algarismo da dezena
String algarismoDaDezena = dezena.toString().substring(0, 1);
//Pega o algarismo da unidade
Integer unidade = soma - (((soma / 10)) * 10);
//Soma o algarismo da dezena com o algarismo da unidade.
soma = Integer.parseInt(algarismoDaDezena) + unidade;
Integer digitoEncontrado = 10 - soma;
//Pega o dígito (último número) do cei digitado.
String digitoCEI = str_cei.substring(11);
if (digitoCEI.equals(digitoEncontrado.toString()))
return true;
else
return false;
}
In PHP the correct is this, which is equivalent to the code in ruby https://pt.stackoverflow.com/a/17534/13508
function cei_valido($cei)
{
$cei = preg_replace('/[^0-9]/', '', $value);
if (strlen($cei) != 12 || $cei == '000000000000') {
return false;
}
$numbers = str_split($cei);
$dv1 = array_pop($numbers);
$multipliers = [7, 4, 1, 8, 5, 2, 1, 6, 3, 7, 4];
for ($i = 0; $i < 11; $i++) {
$numbers[$i] = $numbers[$i] * $multipliers[$i];
}
$sum = array_sum($numbers);
$dv2 = abs(10 - ($sum % 10 + $sum / 10)) % 10;
return $dv2 == $dv1;
}