Javascript validation is not working

Asked

Viewed 190 times

0

I’m stuck in a jam. I made an html form and created a javascript document, but the function I created does not work (as if it was not called) I tried several things and I still can’t.

Here are the codes:

function validar(){

	var d = document.formulario;
	
	if(d.nome.value==""){

		alert("Campo em branco");
		return false;
	}
}
<!DOCTYPE html>
<html>
 <head>
  <title>Aula Prática</title>
  <script type="text/javascript" src="js/formularioScript.js"></script>
  <meta charset="UTF-8">
 </head>
 <body>
  <form id="formulario">
   <div>
    ID: <input type:"text" id="formId" placeholder="Digite sua ID">
    <br></br>
   </div>

   <div>
    Nome: <input type="text" id="nome" placeholder="Digite seu nome">
    <br></br>
   </div>

   <div>
    CPF: <input type="text" id="formCpf" placeholder="Digite deu CPF">
    <br></br>
   </div>
   
   <div>
    Data: <input type="date" id="formData">
    <br></br>
   </div>

   <div>
    Gênero: <input type="integer" id="formGen">
    <br></br>
   </div>

   <div>
    Ano: <input type="integer" id="formAno">
    <br></br>
   </div>

   <div>
    Semestre: <input type="integer" id="formSem">
    <br></br>
   </div>

   <div>
    Número de Disciplinas: <input type="integer" id="formDisci">
    <br></br>
   </div>

   <div>
    Email: <input type="text" id="formEmail">
    <br></br>
   </div>

   <div>
    Cor Administrador: <input type="text" id="formCor">
    <br></br>  
   </div>

   <input type="button" id="validar" value="Confirmar" onclick="validar()">
  </form>
 </body>
</html>

2 answers

1


The function is not being called because there is a conflict enter button id (validar) and the name of the function, which is also validar:

Change the button id to something else.

I adjusted the javascript part according to what I think you want:

function validar(){
  inputs = document.getElementsByTagName('input');
  var missing = 'Falta: ';
  for (index = 0; index < inputs.length; ++index) {
      if(inputs[index].value == '') {
	   missing += inputs[index].id+ ', ';
      }
  }
  if(missing !== 'Falta: ') {
      alert(missing);
  }
}
<form id="formulario">

    ID: <input type="text" id="formId" placeholder="Digite sua ID">
    <br>


    Nome: <input type="text" id="nome" placeholder="Digite seu nome">
    <br>


    CPF: <input type="text" id="formCpf" placeholder="Digite deu CPF">
    <br>
   
    Data: <input type="date" id="formData">
    <br>


    Gênero: <input type="integer" id="formGen">
    <br>


    Ano: <input type="integer" id="formAno">
    <br>

   <div>
    Semestre: <input type="integer" id="formSem">
    <br>


    Número de Disciplinas: <input type="integer" id="formDisci">
    <br>


    Email: <input type="text" id="formEmail">
    <br>


    Cor Administrador: <input type="text" id="formCor">
    <br>

   <input type="button" id="validar_id" value="Confirmar" onclick="validar();return false;">
   </div>
   </form>

  • Thanks for your attention! I made the changes and still not validating, until I redid your code in mine and still will not. I checked if it is no problem in the call of doc js by doc html but it is all ok apparently. :/

  • If you want you can http://www.filedropper.com/ and send you the download link and I’ll take a look

  • You even changed the name of the button id?

  • I did. Both the ID and the "name".

  • Thank you very much, I got it! As you and the others guided me I was making the changes and fixing the errors but still it was not enabled Alert() (with the script inside the html or in a part doc). I was able to print only with the script in the html I identified that what was causing the error was this excerpt: src="js/formularyScript.js". I only had to take out the "js/". Again I thank you for all the help!

  • Okay, good, you can accept the answer that helped you most

Show 1 more comment

0

Always when referring in javascript directly to fields, put the attribute name ="name", because putting only the attribute id it will not locate the object directly, as using Document.formulario.name

Or just use:

  if(document.getElementById("nome").value=="")
       return alert("Campo em branco") && false;
  • Thanks for your attention! I made the changes and still not validating, until I redid your code in mine and still will not. I checked if it is no problem in the call of doc js by doc html but it is all ok apparently. :/

  • It came to work in https://jsfiddle.net/b9m6ajvb/ But it was also necessary to rename the control or function to a different one

  • Thank you very much, I got it! As you and the others guided me I was making the changes and fixing the errors but still it was not enabled Alert() (with the script inside the html or in a part doc). I was able to print only with the script in the html I identified that what was causing the error was this excerpt: src="js/formularyScript.js". I only had to take out the "js/". Again I thank you for all the help!

Browser other questions tagged

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