mmooser, try to put all the functionalities in an array, then use functions like sort
, some
, every
, reduce
to carry out the checks.
var nonNumeric = /[^0-9]/g;
var funcs = [];
var validar = function () {
//normalizando entrada de dados,
//removendo espaços desnecessarios do nome do funcionario
//e caracteres não numericos do CPF.
var normalizado = funcs.map(function (func, indice) {
if (!func.desc) func.desc = "";
if (!func.cpf) func.cpf = "";
func.desc = func.desc.trim();
func.cpf = func.cpf.replace(nonNumeric, "");
return func;
});
//verificando se todos os funcionarios foram preenchidos.
var semDados = normalizado.some(function (func, indice) {
return !func.desc || !func.cpf || func.cpf.length != 11;
});
//ordenando por CPF.
normalizado.sort(function (funcA, funcB) {
return funcA.cpf > funcB.cpf ? 1 : funcA.cpf < funcB.cpf ? -1 : 0;
});
//verificando se há repetição de CPF.
var repetidos = normalizado.reduce(function (repetido, atual, indice, funcs) {
var proximo = funcs[indice+1];
if (!proximo)
return repetido;
return repetido || atual.cpf == proximo.cpf;
}, false);
console.log({ qtd: funcs.length, semDados: semDados, repetidos: repetidos });
};
funcs.push({ desc: "João", cpf: "332.353.141-80" });
funcs.push({ desc: "Maria", cpf: "811.672.633-16" });
funcs.push({ desc: "José", cpf: "954.875.167-40" });
funcs.push({ desc: "Sophia", cpf: "444.765.147-58" });
validar();
// inserindo um novo João, porém com um CPF diferente.
funcs.push({ desc: "João", cpf: "551.812.471-64" });
validar();
// inserindo um novo José, porém com o mesmo CPF, alterando apenas a formatação.
funcs.push({ desc: "José", cpf: "95487516740" });
validar();
//tentativa de inserir funcionando sem nome e sem documento.
funcs.push({ desc: " ", cpf: "..-" });
validar();
in the first validation, checked if a Name has been given to the function and if it is not an empty string, I also check the same for the CPF and if it has 11 numbers.
after checking that all entries have been filled in, I check that all reported Cpfs are single.
doing so, you should have no problems, besides the fact that it will work for whatever number of employees.
qntdemp only has an equal sign, i.e., instead of checking if it is equal, it is setting the value 5.
– Clayderson Ferreira
In addition to what @Claydersonferreira said, if your intention is to say that
qtdemp
must be5
and any of the other options, so these should be between parentheses.if(qtdemp==5 && (true || false || true || true)){}
– Guilherme Lautert
William, I tried to do this to put the == (really had forgotten), but even putting the parentheses I could not do the validation! If I leave this validation along with the others that validate blank field, it gives error in a conversion (which is done after these validations). If I change places for after this conversion, it doesn’t even go through the function. By logic if I wrote: if(employees==1 && (func1.value.length == 0 || cpf1.value.length == 11)){ mgs +="Required Employee Name and CPF<br/>"; } Should work right?
– mmooser