0
I got a Javascript code that fits me perfectly, but it is divided to be used in two inputs
, but I need him to be executed in it input
.
Below is the code. It runs in the event OnKeyUp
, then I wonder if there is the possibility to run the script below in the same input
?
function mascara_data(data)
{
var mydata = '';
mydata = mydata + data;
if (mydata.length == 2)
{
mydata = mydata + '/';
document.forms[0].data.value = mydata;
}
if (mydata.length == 5)
{
mydata = mydata + '/';
document.forms[0].data.value = mydata;
}
if (mydata.length == 10)
{
verifica_data();
}
}
function verifica_data()
{
dia = (document.forms[0].data.value.substring(0, 2));
mes = (document.forms[0].data.value.substring(3, 5));
ano = (document.forms[0].data.value.substring(6, 10));
situacao = "";
// verifica o dia valido para cada mes
if ((dia < 01) || (dia < 01 || dia > 30) && (mes == 04 || mes == 06 || mes == 09 || mes == 11) || dia > 31)
{
situacao = "falsa";
}
// verifica se o mes e valido
if (mes < 01 || mes > 12)
{
situacao = "falsa";
}
// verifica se e ano bissexto
if (mes == 2 && (dia < 01 || dia > 29 || (dia > 28 && (parseInt(ano / 4) != ano / 4))))
{
situacao = "falsa";
}
if (document.forms[0].data.value == "")
{
situacao = "falsa";
}
if (situacao == "falsa")
{
alert("Data inválida!");
document.forms[0].data.focus();
}
}
function mascara_hora(hora)
{
var myhora = '';
myhora = myhora + hora;
if (myhora.length == 2)
{
myhora = myhora + ':';
document.forms[0].hora.value = myhora;
}
if (myhora.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));
alert('hrs ' + hrs);
alert('min ' + min);
situacao = "";
// verifica data e hora
if ((hrs < 00) || (hrs > 23) || (min < 00) || (min > 59))
{
situacao = "falsa";
}
if (document.forms[0].hora.value == "")
{
situacao = "falsa";
}
if (situacao == "falsa")
{
alert("Hora inválida!");
document.forms[0].hora.focus();
}
}
I did not understand your question very well, try to specify more and post the HTML code.
– Gustavo Luciano
this above code I can call <input type="text" name="data" Onkeyup="mascara_data(this.value);" maxlength="10"> dd/mm/yyyy<br> <input type="text" name="data" Onkeyup="mascara_hora(this.value);" maxlength="10"> hh:mm<br> so it works, m need to validate these two inputs in one, so I need to type dd/mm/yyyy hh:mm, and it validates the time, because today I use Mask but Mask does not validate time, it means I can type 28:70, which the input accepts, but as JS above I get this validation.
– Bruno Rayol
Both inputs have the same
name
?– Sam
no, I misspelled, but I do not want two inputs I would like to use in a single this function.
– Bruno Rayol