Javascript "minute second hour" mask

Asked

Viewed 1,154 times

2

Javascript below has mask hh:mm. I need a mask in shape hh:mm:ss.

Does anyone know any solution, or can 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;
}

I appreciate the help.

2 answers

1

It worked, I made some small adjustments, but it illuminated my way, thank you very much.

where was:

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

got:

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

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;
}

Browser other questions tagged

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