Checking if inputs are empty with JavaScript

1

I have the code below that creates three fields in HTML These fields go through the checks with JavaScript , but the part that should check if the person typed something does not work, am I doing it the right way?

function checadatas(){
    var form_ins = document.acess;
    console.log(form_ins);
    var data1 = new Date(form_ins.inp.value);
    var data2 = new Date(form_ins.fip.value);
    if (!data1 || !data2){
      return false;
    }
    if (data1 > data2){
      alert("Data não pode ser maior que a data final");
      return false;
    } else {
      return true;
    }
    if(document.acess.inp.value == null){
            alert("Digite uma data!");
            return(false);
        } else if(document.acess.fip.value == null){
            alert("Digite uma data!");
            return(false);
        } else if(document.acess.ver.value == null){
            alert("Digite uma data!");
            return(false);
        } else {
          return true;
    }
}
<form method="POST" action="#" onSubmit="return checadatas()" name="acess">
<br><br>
<a a class="arib">Data de inicio da verificação: </a><input type="date" name="inp"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<a a class="arib">Data de fim da verificação: </a><input type="date" name="fip"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br><br><br>
<a a class="arib">Data ser realizada a verificação: </a><input type="date" name="ver"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<br><br><br>
       <button class="css_btn_class" type="submit" name="Enviar">Inserir ativo</button>
</form>
    
asked by anonymous 29.05.2018 / 19:42

2 answers

1

Two problems:

These two return s are preventing the rest of the function from being executed. Actually what I left commented is not necessary, because you want to do only negative validation:

if (data1 > data2){
  alert("Data não pode ser maior que a data final");
  return false;
} else {
  // return true;
}

The other are the comparisons with null :

document.acess.inp.value == null

Field values are not null when they are empty. You can check this way:

!document.acess.inp.value // se for vazio, é false

Another point is that you do not have to return true at the end of the function. If the function returns no false , nothing will prevent submit from proceeding.

Code with fixes:

function checadatas(){
    var form_ins = document.acess;
    console.log(form_ins);
    var data1 = new Date(form_ins.inp.value);
    var data2 = new Date(form_ins.fip.value);
    if (!data1 || !data2){
      return false;
    }
    if (data1 > data2){
      alert("Data não pode ser maior que a data final");
      return false;
//    } else {
//      return true;
    }
    if(!document.acess.inp.value){
            alert("Digite uma data!");
            return false;
     } else if(!document.acess.fip.value){
         alert("Digite uma data!");
         return false;
     } else if(!document.acess.ver.value){
         alert("Digite uma data!");
         return false;
     }
}
    
29.05.2018 / 19:59
1

You're checking document.acess.inp.value after the method is finished, "What do you mean?" - Simple, if before check if it is equal to null, you run return no matter if data1 > or < the date2, the call to return ends the method call. To correct it, I believe it is necessary to put the last ìf first in the method.

    
29.05.2018 / 19:55