Checking whether inputs are empty with Javascript

Asked

Viewed 460 times

1

I have the code below that creates three fields in HTML these fields go through the checks below with Javascript, however, the part that should check if the person typed something does not work, I am 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>

  • because the field has content that may not be null, you can compare with empty: document.acess.inp.value == ""

  • I made this exchange, and the program keeps ignoring this part

  • looks well your code seems to me that it would be correct to put the commands if which validate whether it is empty at the beginning of Function. Note that if the field is empty, you may miss this line: var data1 = new date(form_ins.inp.value);`

2 answers

1

Are you checking in document.acess.inp.value after the method has already finished, "How so?" -Simple, in the if before the check if it is equal to null, you run a return it doesn’t matter if data1 > ou < a data2, the call to return ends the method call. To correct, I believe it is necessary to place the last if first of all in the way.

  • That was it, there was no attempt on me, thank you very much!

1


Two problems:

These two returns is preventing the rest of the function from being executed. In fact 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 is the comparisons with null:

document.acess.inp.value == null

The values of the fields 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 don’t need to return true at the end of the function. If the function does not return any false, nothing will prevent the submit proceed.

Code with corrections:

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;
     }
}
  • Had made as a comment above document.acess.inp.value == "" and it had worked, but I found the way you suggested most practical, and it really was the Turn that was catching the method, thank you very much for the help

Browser other questions tagged

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