como validar una entrada de texto con keypress y expresiones regulares sin morir en el intento.
Percentage: {
clazz: 'accepted-keys-percentage',
event: 'keypress',
regex: /^\d{1,2}(?:\.\d+)?$/,
init: function () {
var inputs = document.querySelectorAll('.' + AcceptedKeys.Percentage.clazz);
for (var i = 0; i < inputs.length; i++) {
var inp = inputs[i];
inp.addEventListener(AcceptedKeys.Percentage.event, function (e) {
var k = e.charCode || e.keyCode || 0;
var futureValue = this.value + String.fromCharCode(k);
var accepts = false;
if(futureValue.match(AcceptedKeys.Percentage.regex)){
accepts = true;
}else if(AcceptedKeys.hasDot(futureValue)){
accepts = true;
}else{
accepts = false;
}
if(!accepts){
e.returnValue = false;
e.preventDefault();
}
}, false);
}
}
},
No hay comentarios:
Publicar un comentario