What way can I validate current time

Asked

Viewed 117 times

0

How could develop routine [code block], in order to treat the text field where to insert the Hours in real time (Current time, resulting at the moment). In case the user tries to add hours past - issue warning that he cannot enter time day.

For example:

If now they are 7:00 in the morning, the user will be able to put the 7:00, 7:01 am or 7:02 or plus, but never. introduce least.

This prevents the user(s) from) malicious and/or unsuspecting try to bypass the system - then it will sound like a:

alert('Horário inaceitável. Você atrasou seu danadinho! Insira a HORA atual. Quando chegar o dia do pagamento a gente conversar.rsrs')

Code

function Mascara_Hora(Hora){ 
	var hora01 = ''; 
	hora01 = hora01 + Hora; 
	  if (hora01.length == 2){ 
		hora01 = hora01 + ':'; 
		document.forms[0].Hora.value = hora01; 
	  } 
		if (hora01.length == 5){ 
		  Verifica_Hora(); 
		} 
	  } 
	  
	function Verifica_Hora(){ 
	hrs = (document.forms[0].Hora.value.substring(0,2)); 
	min = (document.forms[0].Hora.value.substring(3,5)); 
				   
	estado = ""; 
	if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){ 
	  estado = "errada"; 
	} 
				   
	if (document.forms[0].Hora.value == "") { 
	  estado = "errada"; 
	} 
	
	if (estado == "errada") { 
	  alert("Hora inválida!"); 
	  document.forms[0].Hora.focus(); 
	  } 
	} 
<input name="Hora" type="text" id="Hora" class="input_text" OnKeyUp="Mascara_Hora(this.value)" size="5" maxlength="5">


However almost ready only this logic remains [accept current time to validate], which I do not know yet.

  • 1

    Then the correct is to validate next to the server as well, because if the user disables the Javascript in the browser is gone.

  • @wmsouza Actually what I want is an example, but this will be done on the server side same.

1 answer

1


Within your method, create variables to store the current time and minute.

var hora = new Date().getHours();
var minuto = new Date().getMinutes();

Then change your condition to check if hrs is less than hora, and if min is less than minuto:

if ((hrs < hora ) || (hrs > 23) || ( min < minuto) ||( min > 59)){ 
  estado = "errada";
}

Working example:

function Mascara_Hora(Hora){
  var hora01 = ''; 
  hora01 = hora01 + Hora; 
  if (hora01.length == 2){ 
    hora01 = hora01 + ':'; 
    document.forms[0].Hora.value = hora01; 
  } 
  if (hora01.length == 5){ 
    Verifica_Hora(); 
  } 
} 

function Verifica_Hora(){
  var hora = new Date().getHours();
  var minuto = new Date().getMinutes();

  hrs = (document.forms[0].Hora.value.substring(0,2)); 
  min = (document.forms[0].Hora.value.substring(3,5)); 

  estado = ""; 
  if ((hrs < hora ) || (hrs > 23) || ( min < minuto) ||( min > 59)){ 
    estado = "errada";
    // Altera o valor do campo com a hora atual
    document.forms[0].Hora.value = hora + ':' + minuto;
  } 

  if (document.forms[0].Hora.value == "") { 
    estado = "errada"; 
  } 

  if (estado == "errada") { 
    alert("Hora inválida!"); 
    document.forms[0].Hora.focus(); 
  } 
} 
<form>
  <input name="Hora" type="text" id="Hora" class="input_text" OnKeyUp="Mascara_Hora(this.value)" size="5" maxlength="5">
</form>

Browser other questions tagged

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