Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Krummelz/6375046 to your computer and use it in GitHub Desktop.
Save Krummelz/6375046 to your computer and use it in GitHub Desktop.
Restricts the characters that are entered into a textbox. Also limits the length of the value to 5. Eg: <input type="text" onkeypress="return inputLimiter(event,'Numbers', this.value)" />
function inputLimiter(e, allow, value) {
var AllowableCharacters = '';
if (allow == 'Letters') { AllowableCharacters = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; }
if (allow == 'Numbers') { AllowableCharacters = '1234567890'; }
if (allow == 'NameCharacters') { AllowableCharacters = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-.\''; }
if (allow == 'NameCharactersAndNumbers') { AllowableCharacters = '1234567890 ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-\''; }
var k;
k = document.all ? parseInt(e.keyCode, 10) : parseInt(e.which, 10);
if (k != 13 && k != 8 && k !== 0) {
if ((e.ctrlKey === false) && (e.altKey === false)) {
return ((AllowableCharacters.indexOf(String.fromCharCode(k)) != -1) && (value.length < 5));
} else {
return true;
}
} else {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment