Function is not running in Javascript

Asked

Viewed 392 times

0

Javascript

function user(){
   input = document.getElementById("user").value;
   label = document.getElementById("tuser");
   if(label.style.display == "block"){
      if(input.length >= 1){
         label.innerHTML = "Usuário deve conter mais 5 caracteres.";
         if(input.length >= 5){
            label.style = "display:none";
         }
      }
   }
}

function pass(){
   input = document.getElementById("pass").value;
   label = document.getElementById("tpass");
   if(label.style.display == "block"){
      if(input.length >= 1){
         label.style = "display:none";
      }
   }
}

Both are not running, so it returns an error in the Chrome Console

(index):225 Uncaught TypeError: pass is not a function
    at HTMLInputElement.onkeypress ((index):225)

HTML (calls function)

<input type="text" name="usuario" id="user" onkeypress="user()" autofocus maxlength="12" value="" class="form-control" placeholder="Seu Usuário">
<input type="password" name="senha" id="pass" onkeypress="pass()" maxlength="16" class="form-control" placeholder="Sua Senha">

Function will make change in

<font color="#ff0000" id="tuser" class="animation-slideUp inserted" style="display:none;">Insira seu usuário.</font>
<font color="#ff0000" id="tpass" class="animation-slideUp inserted" style="display:none;">Insira sua senha.</font>

I found it very strange that this happened, because the codes are "correct" (I think). I ran them on the console and there was no error.

  • 2

    Were these JS functions defined in the HTML file itself or in a separate JS file? If it is separate, how did you include the JS file in the HTML file?

  • Defined in HTML file itself.

  • Can you enter the full code? In fact, avoid using the element font. It has been obsolete since HTML 4.

  • Progressing, apart from the code, tell us what you want with this code, maybe we can help you with logic, because I realized there’s enough unnecessary stuff. ;)

  • 1

    Your problem is some error on a line before these functions. So the script did not load them into memory and in practice they became nonexistent. So it’s good to post the full code to analyze.

  • After sending the form the javascript checks if the fields have been filled in, if the field is not filled in an error message will appear below the input (in this case the label). So if you type in the input the message will disappear, and in the user after the first digit will appear an error that needs to be more than 5 characters, then type the 5 and the error disappears.

  • Function validate(){ var Valid = true; if (Document.formlog.usuario.value == ""){ Document.getElementById('tuser'). style = "display:block;"; Valid = false; } if(Document.formlog.password.value == ""){ Document.getElementById('tpass'). style = "display:block;"; Valid = false; } if(Valid == true){ Loader(); } Return Valid; }

Show 2 more comments

1 answer

-1

The order in which the JS script is referenced in HTML matters.

Inserts a.log console before the functions and is seen in the browser command line (F12 in principle/ Inspect element > Console) appears. If it appears it is because it is calling the file, if it is not shown, check the paths.

"onkeypress" only makes sense if you have a script with the loading done, you have to open a giant function in the script with "Document.onload" or "onready" and start calling the functions inside. The browser running the DOM will first load the JS scripts before any loading concrete (aka, HTML and CSS) of your page and if that happens you will not be able to call any function from there.

$( document ).ready(function() {
     //funcoes ca para dentro a serem chamadas
    console.log( "ready!" ); //se este console já der, é porque te faltava isto do document
})

this code is in Jquery, so you should have a Jquery library to call before any other script your JS

  • $( Document ).ready(Function() { WILL I PUT THE FUNCTIONS HERE? EX: Function user() { } })

  • Yes, but try the.log console first and see if it appears on your browser console. If it does, then you can work your script from there. However, to do what you want, I advise you to use frameworks. There are plenty of front-end frameworks that help you do these things and with concrete examples.

  • It didn’t do any good.

  • But no console.log appeared on the browser console?

Browser other questions tagged

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