Validating two date fields

1

I need to verify that the user has entered the date in two date fields.

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

How to solve this? If I put required in the field, it only works in chrome. But what about 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>
    
asked by anonymous 06.05.2015 / 20:48

2 answers

1

Make sure the fields are empty, you 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 (action.php)

<?php

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

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

?>
    
06.05.2015 / 22:01
1

The error in both the author's question and the response of Renaro Santos ( em javascript ) is in this line

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

exactly in data2=""

The correct one 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>
    
26.08.2017 / 14:43