Mask "minute minute second" in JavaScript

2

JavaScript below has hh:mm mask. I need a mask in hh:mm:ss format.

Does anyone know of a solution, or can you help me adapt the code below?

function formatHora(campo, e)
{
  if (!e)
    return false;

  car = (window.Event) ? e.which : e.keyCode;

  if (car == 8)
    return true;

  if((((car >=48)&&(car <=57))||(car == 8)) && (campo.value.length < 7))
  {
    if (campo.value.length == 2)
      campo.value = campo.value + ':';

//    campo.value = campo.value + ':';

      return true;
    }
    return false;
}

Thanks for the help.

    
asked by anonymous 02.03.2016 / 03:58

2 answers

1

It worked, I made some minor adjustments, but it brightened my path, thank you very much.

Where I was:

if((((car >=48)&&(car <=57))||(car == 8)) && (campo.value.length < 9)){

stayed:

if((((car >=48)&&(car <=57))||(car == 8)) && (campo.value.length < 8)){
    
02.03.2016 / 15:10
0
function formatHora(campo, e){

if (!e){ return false; }

car = (window.Event) ? e.which : e.keyCode;

if (car == 8){ return true; }

if((((car >=48)&&(car <=57))||(car == 8)) && (campo.value.length < 9)){

  /* É SÓ ACRESCENTAR ESSE (|| campo.value.length == 5) NO IF ABAIXO */

  if (campo.value.length == 2 || campo.value.length == 5){
    campo.value = campo.value + ':';
  }

  return true;
}
return false;
}
    
02.03.2016 / 04:28