Validating two date fields

Asked

Viewed 594 times

1

I need to check if the user entered the date in two date fields.

<input type="text" name="data1"/>
<input type="text" name="data2"/>

How do we fix this? If I put required in the field, it only works on Chrome. But and pro Firefox and Safari?

See @Renaro Santos

<script> 
function validar(){ 
var data1= document.getElementById("dataInicio").value; 
var data2= document.getElementById("dataFim").value; 
if(data1 == "" || data2=""){ 
alert('Uma das datas esta vazia'); 
return false; 
} 
} 
</script>
<form method="post" action="" onsubmit="validar()"> 
Data inicial<input type="text" id="dataInicio" name="dataInicio" required> 
Data Final<input type="text" id="dataFim" name="dataFim" required>
<input type="submit" name="pesquisar" value="Pesquisar">
</form>
  • 1

    the input of the kind date has not yet been implemented in all browsers so its use is not recommended, take a look at this jQuery plugin for validation: http://formvalidator.net/

  • Sorry, I’m actually using type="text" and not date.

2 answers

1

The error both in the author’s question and in the answer of Renaro Santos (em javascript) is on that line

if(data1 == "" || data2=""){

exactly in data2=""

The correct is data2 ==

if(data1 == "" || data2 == ""){

function validar(){ 
var data1= document.getElementById("dataInicio").value; 
var data2= document.getElementById("dataFim").value; 
if(data1 == "" || data2 == ""){ 
   alert('Uma das datas esta vazia'); 
   return false; 
} 
}
<form method="post" action="" onsubmit="validar()"> 
Data inicial<input type="text" id="dataInicio" name="dataInicio" required> 
Data Final<input type="text" id="dataFim" name="dataFim" required>
<input type="submit" name="pesquisar" value="Pesquisar">
</form>

1

Check that the fields are empty, Voce can do this using Javascript and PHP.

Put ID’s in the elements:

<script>
    function validar(){
    var data1= document.getElementById("data1").value;
    var data2= document.getElementById("data1").value;

    if(data1 == "" || data2=""){
       alert('Uma das datas esta vazia');
       return false;
       }
    return true;

    }
</script>

 <form method="POST" action="action.php" onsubmit="validar()">
      <input type="text" name="data1" id="data1"/>
      <input type="text" name="data2" id="data2"/>
    </form>

PHP (php action.)

<?php

$data1=$_POST['data1'];
$data2=$_POST['data2'];

if($data1 =="" || $data2 ==""){
  echo 'Uma das datas esta vazia';
  return;
  }

?>
  • Got it @Renaro Santos, I’ll try.

  • didn’t work! How can I put my code here to get it out formatted?

  • Try now @Gustavosevero, if it doesn’t work, post your code and more details...

  • See @Renaro Santos { <script> Function validar(){ var data1= Document.getElementById("dataInicio"). value; var data2= Document.getElementById("dataFim"). value; if(data1 == "" || data2=""){ Alert('One of the dates is empty'); Return false; } } </script> <form method="post" action="" onsubmit="validate()"> Start date<input type="text" id="dateInicio" name="dateInicio" required> End Date<input type="text" id="dateFim" name="dateFim" required><input type="type input name="name"Search" ="Search"value"> </form> }

  • @Gustavosevero, put this code in the question to make it easy to see...

Browser other questions tagged

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