Created
June 21, 2010 18:05
-
-
Save JadedEvan/447235 to your computer and use it in GitHub Desktop.
Javascript onkeup() function that prevents entering more than X characters
This file contains 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 charsRemaining(evt, counterID, max) { | |
var chars = evt.getValue(); | |
if (chars.length == 1 && chars[0] == '') { chars = []; } | |
var remaining = max - chars.length; | |
var counter = document.getElementById(counterID); | |
if (remaining >= 0) { | |
if(counter) counter.setTextValue(remaining + ' characters remaining'); | |
} | |
else { | |
evt.setValue(evt.getValue().substr(0, max)); | |
if(counter) counter.setTextValue((remaining * -1) + ' characters over'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment