Error calling Function on my page

Asked

Viewed 65 times

2

I’m having trouble calling two functions to validate the fields txtnome and txtsobrenome, calling the roles validanome() and validasobrenome().

I tried to call them the following way:

if (!validanome(cad.txtnome.value)) {
    return;  
}
if (!validasobrenome(cad.txtsobrenome.value)) {
    return;  
}

The entire code of my page and script is here.

There are two functions, in Javascript.

I would like someone to briefly analyze and find out why it’s going wrong, what I’m failing. The functions validanome and validasobrenome that I quoted meet there at the end of the script, and I’m trying to call it in my Function validaFormulario().

Note: I took the else at the end of Function validanome() and validasobrenome(), but it hasn’t worked yet.

2 answers

2


Include the ids id="txtnome" and id="txtsobrenome" in the fields name and surname respectively:

<input type="text" name="txtnome" id="txtnome" maxlength="40" required />

and

<input type="text" id="txtsobrenome" name="txtsobrenome" maxlength="30" required />

In the functions to validate these fields, it is necessary to put return false when it is invalid and return true when valid:

function validanome() {
   var nome = document.getElementById("txtnome").value;
   var padrao = /[^a-zà-ú]/gi;
   var valida_nome = nome.match(padrao);
   if( valida_nome || !nome ){
      alert("Nome possui caracteres inválidos ou é vazio, por favor, preencha-o corretamente.");
      return false;
   }
   return true;
}

function validasobrenome() {
   var sobrenome = document.getElementById("txtsobrenome").value;
   var padrao = /[^a-zà-ú]/gi;
   var valida_sobrenome = sobrenome.match(padrao);
   if( valida_sobrenome || !sobrenome ){
      alert("Sobrenome possui caracteres inválidos ou é vazio, por favor, preencha-o corretamente.");
      return false;
   }
   return true;
}

Other functions are also in trouble, such as muitoCurto(), for example. It should be like this:

function muitoCurto(campo, nome, tamanho) {
   if (campo.value.length < tamanho){
      alert("O conteúdo do campo '" + nome
      + "' deve ter pelo menos " + tamanho + " caracteres."
      + " Por favor, preencha-o corretamente.");
      return false;
   }
}

Then you adjust the other functions in the same way.

1

In the functions you want to call, there is already the call to the element that will be filtered and does not have the parameter in which would be informed which element should be filtered:

var nome = document.getElementById("txtnome").value;
var sobrenome = document.getElementById("txtsobrenome").value;

So the call should just be validanome() and validasobrenome() no parameters. However, the returns tbm do not agree with what you specified in if()s, the functions should remain so, without the parameters:

function validanome() {
    var nome = document.getElementById("txtnome").value;
    var padrao = /[^a-zA-Zà-úÀ-Ú]/gi;
    var valida_nome = nome.match(padrao);
    if( valida_nome || !nome ){
        return false;
    }else{
        return true;
    }
}

function validasobrenome(){
    var sobrenome = document.getElementById("txtsobrenome").value;
    var padrao = /[^a-zA-Zà-úÀ-Ú ]/gi;
    var valida_sobrenome = sobrenome.match(padrao);
    if( valida_sobrenome || !sobrenome ){
      return false;
    }else{
      return true;
    }
}

And the if()s for functions without parameters would be like this:

if (!validanome()){
     //codigo de retorno de erro
}
if (!validasobrenome()){
     //codigo de retorno de erro
}

Or with parameters:

function validanome(nome) {
    var padrao = /[^a-zA-Zà-úÀ-Ú]/gi;
    var valida_nome = nome.match(padrao);
    if( !valida_nome || !nome ){
        return false;
    }else{
        return true;
    }
}

function validasobrenome(sobrenome){
    var padrao = /[^a-zA-Zà-úÀ-Ú ]/gi;
    var valida_sobrenome = sobrenome.match(padrao);
    if( !valida_sobrenome || !sobrenome ){
      return false;
    }else{
      return true;
    }
}

And the if()s of the functions with parameters:

if (!validanome(document.getElementById("txtnome").value)){
     //codigo de retorno de erro
}
if (!validasobrenome(document.getElementById("txtsobrenome").value)){
     //codigo de retorno de erro
}
  • 1

    I didn’t understand very well, it’s for me to exchange the two functions for which you changed there and then change the if as well?

  • There are two exeplos of how the functions can run, in their functions, you already called the items by the id within the function, so you didn’t need to declare the if as you were doing, it should be as reported in my first example

  • 1

    What do I have to do for him to spin?

  • 1

    swap my Function for these q vc did: Function validanome(name) { var padrao = /[ a-za-Zà-úAt-Ú]/gi; var valida_nome = name.match(default); if( !valida_name || !name ){ Return false; }Else{ Return true; } } Function validasurname(surname){ var default = /[ a-za-Zà-Ú ]/gi; var valida_surname = surname.match(default); if( !valida_surname || !surname ){ turn false; }Else{ Return true; } } and then change the if?

  • use one of the two examples, in the first, you call the function inside the if so if (!validanome()) because the function itself will fetch the object to be checked

  • yes, you exchange the functions for one of the ones I showed you and the ifs tbm

  • I used the two examples but is giving error yet, not calling the error

  • the dvd helped me with the missing problem, thanks for the expensive solution, you solved my problem

Show 3 more comments

Browser other questions tagged

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