Problem with JavaScript validation

0

I'd like some help finding out what's wrong with this validation:

if(qtdemp=5 && func1.value.length == 0 || cpf1.value.length == 0 || func2.value.length == 0 || cpf2.value.length == 0 || func3.value.length == 0 || cpf3.value.length == 0 || func4.value.length == 0 || cpf4.value.length == 0 || func5.value.length == 0 || cpf5.value.length == 0){
    mgs +="Nome e CPF do(s) Funcionário(s) obrigatórios<br/>";
}
The intent is to check whether qtdemp=5 and whether the func1 or cpf1 or func2 or cpf2 or func3 or cpf3 or func4 or cpf4 or% or func5 are empty.

Similar validations (if cpf5 = func1 for example) are also done next.

Whenever I activate these validations it gives error in my code (in a part that has nothing to do with this). Just get them out that the recording works.

    
asked by anonymous 07.03.2016 / 15:36

1 answer

1

mmooser, try to put all the officials in an array, then use functions like sort , some , every , reduce to perform 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 was given to the official and if it is not an empty string, I also check the same for the CPF and if it has 11 numbers.

After verifying that all the entries have been filled in, I check that all the CPFs entered are unique.

In this way, you should have no problems, besides the fact that it will work for any number of officials.

    
07.03.2016 / 16:34