Skip to content

Instantly share code, notes, and snippets.

@zxmarcos
Created August 16, 2013 04:28
Show Gist options
  • Select an option

  • Save zxmarcos/6247332 to your computer and use it in GitHub Desktop.

Select an option

Save zxmarcos/6247332 to your computer and use it in GitHub Desktop.
function inserir(t, index, str) {
return t.substring(0, index) + str + t.substring(index, index + t.length);
}
function trocar(t, index, str) {
return t.substr(0, index) + str + t.substr(index+str.length);
}
function validarHora(evt, controle) {
var evento = evt || window.event;
var tecla = evento.keyCode || evento.which;
var input = document.getElementById(controle);
tecla = String.fromCharCode(tecla);
/* Expressões regulares para formar um padrão de hora */
var regex = new Array(
/^[0-2]$/,
/^(([0-1][0-9])|(2[0-3]))$/,
/^(([0-1][0-9])|(2[0-3])):$/,
/^(([0-1][0-9])|(2[0-3])):([0-5])$/,
/^(([0-1][0-9])|(2[0-3])):([0-5][0-9])$/,
/^(([0-1][0-9])|(2[0-3])):([0-5][0-9]):$/,
/^(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5])$/,
/^(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])$/
);
var texto = document.getElementById(controle).value;
var pos = input.selectionStart;
texto = inserir(texto, pos, tecla);
document.getElementById("testador").innerHTML = texto;
for (var i = 0; i < regex.length; i++) {
if (regex[i].test(texto)) {
document.getElementById("label").innerHTML = "Aceito" + pos;
return true;
}
}
document.getElementById("label").innerHTML = "Rejeitado" + pos;
if (evento.preventDefault)
evento.preventDefault();
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment