Posted By

on 04/22/09


Tagged

javascript


Versions (?)

Limit input text, the easy way


 / Published in: JavaScript
 

URL: http://www.webcodingtech.com/javascript/limit-input-text.php

  1. HTML
  2. ---------
  3. <form>
  4. <textarea cols=100 rows=20 onkeyup="change(this);"></textarea>
  5. <br>You've typed <span id="char_cnt">0</span> character(s). You
  6. are allowed <span id="chars_left">lots</span> more.
  7. </form>
  8. ---------
  9.  
  10. JAVASCRIPT
  11. ---------
  12. <script type="text/javascript">
  13. function change (el) {
  14. var max_len = 10;
  15. if (el.value.length > max_len) {
  16. el.value = el.value.substr(0, max_len);
  17. }
  18. document.getElementById('char_cnt').innerHTML = el.value.length;
  19. document.getElementById('chars_left').innerHTML = max_len - el.value.length;
  20. return true;
  21. }
  22. </script>
  23. ---------

Report this snippet  

You need to login to post a comment.