How can I validate current time

0

How could you develop routine [code block], in order to treat the text field where you have to insert the Hours in real time (current time, arising from the moment). If the user tries to add hours past - issue warning that can not enter daylight hours .

For example:

If they are 7:00 in the morning, the user can put 7:00, 7:01 or 7:02 or more >, but never enter less .

This prevents malicious and / or unsuspecting user (s) from attempting to circumvent the system - then it will sound 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, it is almost ready to accept this logic [accept current time to validate], which I do not know yet.

    
asked by anonymous 19.04.2018 / 22:57

1 answer

1

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

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

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

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

Example running:

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>
    
19.04.2018 / 23:17