Skip to content

Instantly share code, notes, and snippets.

@asimmittal
Created February 12, 2017 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asimmittal/6d7032cca216e2414be3bf958dd8ec17 to your computer and use it in GitHub Desktop.
Save asimmittal/6d7032cca216e2414be3bf958dd8ec17 to your computer and use it in GitHub Desktop.
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){
e.preventDefault();
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment