I have two fields of type date
in my form, the field data_inicio
and data_fim
.
I need to set a script
that when I finish filling the dates, that is when the fields lose their focus, an alert is sent, warning if data_inicio
is greater than data_fim
, data_inicio
can never be greater than data_fim
.
I've already done a script using the onclik
event, but it only warns me when I click on submit
.
Follow the code below:
function comparadatas()
{
var data_inicio = document.getElementById("data_inicio").value;
var data_fim = document.getElementById("data_fim").value;
if(data_inicio > data_fim){
alert("ERRO! A DATA DE NOTICAÇÃO NAO PODE SER MAIOR DO QUE A DATA DE COMPARECIMENTO");
}
}
<form>
<label for="data_inicio">Data Inicio</label>
<input type="date" name="data_inicio" id="data_inicio"><br/>
<label for="data_fim">Data Final</label>
<input type="date" name="data_fim" id="data_fim"><br/>
<input type="submit" value="REALIZAR CADASTRO" onclick="comparadatas()">
</form>