Created
October 31, 2012 02:44
-
-
Save artursapek/3984504 to your computer and use it in GitHub Desktop.
Character whitelisting for input field
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
(function($){ | |
// Bind to an input field. Provide chars, a string of characters. | |
// Now only those characters can be typed in. | |
$.fn.whitelist = function(chars){ | |
var $this = $(this), chars = chars.split(''); | |
$this.keypress(function(e){ | |
if (chars.indexOf(String.fromCharCode(e.which)) == -1){ return false; }}); | |
return $this; | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment