Problem with Javascript validation

Asked

Viewed 115 times

0

I would like a help to know 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 intention is to verify whether qtdemp=5 and whether the fields func1 or cpf1 or func2 or cpf2 or func3 or cpf3 or func4 or cpf4 or func5 or cpf5 are empty.

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

Whenever I activate these validations gives error in my code (in a part that has nothing to do with it). Just take them off it works saving.

  • 3

    qntdemp only has an equal sign, i.e., instead of checking if it is equal, it is setting the value 5.

  • 1

    In addition to what @Claydersonferreira said, if your intention is to say that qtdemp must be 5 and any of the other options, so these should be between parentheses. if(qtdemp==5 && (true || false || true || true)){}

  • 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?

1 answer

1

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.

  • Toby, I think this validation is a lot for me! Because it does not exceed the limit of 5 names and 5 cpfs. I’m totally new to programming, I’m breaking! Thanks anyway!!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.