Skip to content

Instantly share code, notes, and snippets.

@lucasmazza
Created March 11, 2010 14:32
Show Gist options
  • Select an option

  • Save lucasmazza/329162 to your computer and use it in GitHub Desktop.

Select an option

Save lucasmazza/329162 to your computer and use it in GitHub Desktop.
// Minimal MaxLength validation on jQuery
// Expected markup : <textarea class="maxlen 250"></textarea>
jQuery("textarea.maxlen").live("keyup", function(event) {
var max = parseInt(this.className.match(/\d+/));
var key = event.which;
if (max && (key >= 33 || key == 13)) {
if (this.value.length >= max) {
this.value = this.value.substr(0, max);
event.preventDefault();
}
}
});
// Numeric only input
jQuery("input.numerical").live("keyup", function(event) {
var key = event.which;
jQuery(this).val(this.value.replace(/\D/ig, ""));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment