Skip to content

Instantly share code, notes, and snippets.

@arnekolja
Created October 22, 2010 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arnekolja/640640 to your computer and use it in GitHub Desktop.
Save arnekolja/640640 to your computer and use it in GitHub Desktop.
Simplest way to allowing only given regular expressions in an input field. Uses jQuery for selecting the element though, you might want to change this.
$(document).ready( function() {
$("input.alphanumeric").keypress(function (e) {
if (String.fromCharCode(e.keyCode).search(/[0-9A-Za-z]/) == -1) return false;
});
$("input.numeric").keypress(function (e) {
if (String.fromCharCode(e.keyCode).search(/[0-9]/) == -1) return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment