Skip to content

Instantly share code, notes, and snippets.

@artursapek
Created October 31, 2012 02:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artursapek/3984504 to your computer and use it in GitHub Desktop.
Save artursapek/3984504 to your computer and use it in GitHub Desktop.
Character whitelisting for input field
(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