I cannot handle properly some events in input type="text". I just need a filter to only accept [^0-9] chars.
I need the following help:
- Do not accept any content coming from Control + Paste command. Either from Control+V (keyboard) or Right click mouse + Paste Option.
What i tried so far:
keypress event i handle nicely any input from keyboard. (any chars not in [^0-9] and enter, backspace, home, end, etc will be simply ignored).
keyup i handle weirdly Control + Paste (keyboard) event. I paste some string, and after the string is pasted, i crop any non-white list chars. PS: BUT this don't work with Right Click + Paste (mouse), neither
onChange the garbage string stay visible until the user blur the field.
What i want:
Copy the example string "123.321.456-78" and paste "12332145678" or "abc!2¨#7" and paste "27".
Do not accept any non-white list chars from any possible way. (even with $('#field').val("trash input 123").
From all problems above, i can handle nicely or weirdly (aka: keuup) the input, but Right Click + Paste (mouse) NEVER trigger ANY EVENT so i can do the properly treatment.
I thought about doing Interval Check, but this is too ugly.
EDIT:
Code below
function soNumeroInteiro_keypressHandler(event)
{
var code = event.keyCode || event.which;
switch(code)
{
case 8: // backspace
case 37: // left
case 39: // right
case 9: // tab
case 46: // delete
case 35: // end
case 36: // home
return true;
break;
case 86: // control + A
case 97: // control + V
case 120: // control + X
if (event.ctrlKey) {
return true;
}
break;
}
var tecla = String.fromCharCode(code);
return tecla.match(/^\d$/) != null;
}
function soNumeroInteiro_keyupHandler(event)
{
event.currentTarget.value = event.currentTarget.value.replace(/[^0-9]/g, '');
}