validate length of string

/ Published in: JavaScript
Save to your folder(s)

this function returns if the passed string is shorter than the limit or not
when you press keys other than F5, backspace, or any arrow keys and the limit is reached it shows an alert.

use with isValueInArray function

Copy this code and paste it in your HTML
  1. function validateLength(event, limit, str) {
  2. notTooLong = true;
  3. okKeyCodes = new Array(8, 116, 37, 38, 39, 40);
  4. if (str.length >= limit && !(isValueInArray(okKeyCodes, event.keyCode))) {
  5. alert("text area limit reached");
  6. notTooLong = false;
  7. }
  8. return notTooLong;
  9. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.