Skip to content

Instantly share code, notes, and snippets.

@kdarty
Last active September 15, 2016 16:02
Show Gist options
  • Save kdarty/b8735c49119f3825296c5e451731a91a to your computer and use it in GitHub Desktop.
Save kdarty/b8735c49119f3825296c5e451731a91a to your computer and use it in GitHub Desktop.
While Input Type TEXT can easily be limited to input based on the Maxlength, Input Type NUMBER does not. To fix this shortcoming and keep the user-friendly experience of moving to a Numeric Keyboard on Mobile, trap the "KeyUp" Event and slice input text based on the given Maxlength of the field.
// On Key Up Event Handler to ensure Numeric Inputs adhere to their Max Length
$('input[type=number]').on('keyup', function (event) {
var maxlength = $(this).attr('maxlength');
if (this.value.length > maxlength) {
this.value = this.value.slice(0, maxlength);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment