Skip to content

Instantly share code, notes, and snippets.

@collin
Created October 5, 2008 00:05
Show Gist options
  • Save collin/14828 to your computer and use it in GitHub Desktop.
Save collin/14828 to your computer and use it in GitHub Desktop.
$('.zipcode').whitelist(/\d/);
$('.state').whitelist(/[A-Z]/);
$('.plain').blacklist(/[!.,<>();:-_]/);
(function($) {
function in_list(e, expr) {
return e.charCode > 0
&& String.fromCharCode(e.which).match(expr);
}
$.fn.whitelist = function(expr) {
return this.keypress(function(e) {
if(!in_list(e, expr)) e.preventDefault();
});
}
$.fn.blacklist = function(expr) {
return this.keypress(function(e) {
if(in_list(e, expr)) e.preventDefault();
});
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment