/ Published in: JavaScript
Restricts the characters that are entered into a textbox. Also limits the length of the value to 5.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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) : parseInt(e.which); 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; } } <input type="text" onkeypress="return inputLimiter(event,'Numbers', this.value)" />