Created
January 28, 2013 22:56
-
-
Save rickycheers/4660074 to your computer and use it in GitHub Desktop.
Callback implementation for only allowing the insertion of numbers on an input.
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
$('#selector').on('keydown', onlyNumber); | |
function onlyNumber(event){ | |
// Allow: backspace, delete, tab, escape, and enter | |
if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || | |
// Allow: Ctrl+A | |
(event.keyCode == 65 && event.ctrlKey === true) || | |
// Allow: home, end, left, right | |
(event.keyCode >= 35 && event.keyCode <= 39) || | |
// Allow point | |
(event.keyCode == 190) | |
) { | |
// let it happen, don't do anything | |
return; | |
} else { | |
// Ensure that it is a number and stop the keypress | |
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) { | |
event.preventDefault(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Con jQuery
Con JavaScript
ejemplo: