Created
February 12, 2017 01:03
-
-
Save asimmittal/6d7032cca216e2414be3bf958dd8ec17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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