Created
August 29, 2013 07:15
-
-
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)" />
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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