Incompatibility between onkeyup and date mask?

2

I have a problem using onkeyup in a date mask that is as follows: when I try to use backspace to modify the date, I can not get past the bar (just hold the button pressed). The bar disappears and pops up (test here in fiddle , or below under "Run snippet", type a date any and then try to delete).

This only happens if I am using onkeyup , with onkeypress is normal (but I have to use onkeyup , because it also serves to enable a button if the date validates, and it has to be when typing, and not by losing focus ...).

     function formatar(mascara, documento) {
                var i = documento.value.length;
                var saida = mascara.substring(0, 1);
                var texto = mascara.substring(i);
                if (texto.substring(0, 1) != saida) {
                    documento.value += texto.substring(0, 1);
                }
            }
<input class="form-control" type="text" maxlength="10" placeholder="dd/mm/aaaa" onkeyup="formatar('##/##/####', this)" name="Tinsem3" id="Cinsem">

Is there a way?

    
asked by anonymous 17.06.2015 / 07:02

1 answer

1

You can make the function not run if the pressed key is backspace or delete . In this case you need to pass the event to the function and then look for whether the key code is 8 or resp. 46.

onkeyup="formatar('##/##/####', this, event)"

and in JavaScript

function formatar(mascara, documento, e) {
    var code = e.keyCode || e.code;
    if (code == 8 || code == 46) return;

jsFiddle: link

    
17.06.2015 / 09:15