Posted By

hotdiggity on 03/30/09


Tagged

css javascript html


Versions (?)

Limit Word Count in a textarea


 / Published in: JavaScript
 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Strict//EN"
  2. "http://www.w3.org/TR/html4/strict.dtd">
  3. <html lang="en" dir="ltr">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <title>Limit Word Count in a textarea</title>
  7. <script type="text/javascript"><!--
  8. var wordLen = 10; // Maximum word length
  9. function checkWordLen(obj){
  10. var len = obj.value.split(/[\s]+/);
  11. if(len.length > wordLen){
  12. alert("You've exceeded the "+wordLen+" word limit for the article!");
  13. obj.oldValue = obj.value!=obj.oldValue?obj.value:obj.oldValue;
  14. obj.value = obj.oldValue?obj.oldValue:"";
  15. return false;
  16. }
  17. return true;
  18. }
  19. // --></script>
  20. </head>
  21. <body>
  22. <h1>JavaScript Example</h1>
  23. <p>This JavaScript sets a maximum word count to a textarea.</p>
  24. <form action="" method="get">
  25. <fieldset>
  26. <legend>Example Form</legend>
  27. <label>Text input: <br><textarea rows="15" cols="30" name="t1" onkeyup="checkWordLen(this);"></textarea></label>
  28. <input class="button" name="Submit" type="submit" value="Submit" />
  29. <input class="button" name="Reset" type="reset" value="Reset" />
  30. </fieldset>
  31. </form>
  32. </body>
  33. </html>

Report this snippet  

You need to login to post a comment.