Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
var filter = [];
//since we're looking for phone numbers, we need
//to allow digits 0 - 9 (they can come from either
//the numeric keys or the numpad)
const keypadZero = 48;
const numpadZero = 96;
//add key codes for digits 0 - 9 into this filter
for(var i = 0; i <= 9; i++){
filter.push(i + keypadZero);
filter.push(i + numpadZero);
//add other keys that might be needed for navigation
//or for editing the keyboard input
filter.push(8); //backspace
filter.push(9); //tab
filter.push(46); //delete
filter.push(37); //left arrow
filter.push(39); //right arrow
* onKeyDown(e)
* when a key is pressed down, check if it is allowed
* or not. If not allowed, prevent the key event
* from propagating further
function onKeyDown(e){
if(filter.indexOf(e.keyCode) < 0){
return false;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment