I found this functional example, validates both the date in the dd/mm/yyyy format and does not allow characters other than numbers, just adapt your input
for the function, thus:
function mascaraData(val) {
var pass = val.value;
var expr = /[0123456789]/;
for (i = 0; i < pass.length; i++) {
// charAt -> retorna o caractere posicionado no índice especificado
var lchar = val.value.charAt(i);
var nchar = val.value.charAt(i + 1);
if (i == 0) {
// search -> retorna um valor inteiro, indicando a posição do inicio da primeira
// ocorrência de expReg dentro de instStr. Se nenhuma ocorrencia for encontrada o método retornara -1
// instStr.search(expReg);
if ((lchar.search(expr) != 0) || (lchar > 3)) {
val.value = "";
}
} else if (i == 1) {
if (lchar.search(expr) != 0) {
// substring(indice1,indice2)
// indice1, indice2 -> será usado para delimitar a string
var tst1 = val.value.substring(0, (i));
val.value = tst1;
continue;
}
if ((nchar != '/') && (nchar != '')) {
var tst1 = val.value.substring(0, (i) + 1);
if (nchar.search(expr) != 0)
var tst2 = val.value.substring(i + 2, pass.length);
else
var tst2 = val.value.substring(i + 1, pass.length);
val.value = tst1 + '/' + tst2;
}
} else if (i == 4) {
if (lchar.search(expr) != 0) {
var tst1 = val.value.substring(0, (i));
val.value = tst1;
continue;
}
if ((nchar != '/') && (nchar != '')) {
var tst1 = val.value.substring(0, (i) + 1);
if (nchar.search(expr) != 0)
var tst2 = val.value.substring(i + 2, pass.length);
else
var tst2 = val.value.substring(i + 1, pass.length);
val.value = tst1 + '/' + tst2;
}
}
if (i >= 6) {
if (lchar.search(expr) != 0) {
var tst1 = val.value.substring(0, (i));
val.value = tst1;
}
}
}
if (pass.length > 10)
val.value = val.value.substring(0, 10);
return true;
}
<input type="text" name="outra_data" id="outra_data" maxlength="10" onkeypress="mascaraData(this)" />
Code reference:
http://forum.imasters.com.br/topic/256533-fazer-mascara-para-text-field-para-ddmmaaaa/
Regex is what you need!
– user28595
More like I can use this ?
– user38360
Related: How to format date in javascript?
– user28595
Take a look at the last code of this answer.
– gustavox
i want to use Javascript only
– user38360