javascript validation is not working

0

I'm stuck in a problem. I made a html form and created a javascript document, but the function I created did not work (as if I was not even calling it) I already tried several things and I still can not.

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>
    
asked by anonymous 01.06.2016 / 17:13

2 answers

1

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

Change the button id to something else.

I set the javascript part to whatever it seems I 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>
    
01.06.2016 / 17:37
0

Always when referring in javascript directly to fields, put the attribute name="name", because by placing only the id attribute it will not find the object directly, like using document.formula.name

Or just use:

  if(document.getElementById("nome").value=="")
       return alert("Campo em branco") && false;
    
01.06.2016 / 19:39