Skip to content

Instantly share code, notes, and snippets.

@JadedEvan
Created June 21, 2010 18:05
Show Gist options
  • Save JadedEvan/447235 to your computer and use it in GitHub Desktop.
Save JadedEvan/447235 to your computer and use it in GitHub Desktop.
Javascript onkeup() function that prevents entering more than X 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