Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word limit in text box

Status
Not open for further replies.

hunter13605

Technical User
Apr 7, 2006
44
0
0
US
Is there a way to limit the number of words allowed? I am able to limit the number of characters but not sure how (or if it is possible) to limit the number of words.
 
it is possible sort of, one way could be:

Code:
@words = split(/\s+/,$textbox);

This creates an array of "words" based on splitting the text on the spaces between words. Now check how many elements are in @words:

Code:
print "Error: Too many words" if (@words > 200);



 
Thanks kevin, you've been a great help today. I'm just about done with the project. In the process of this project, it being my first, i have learned a tremendouse amount of informatoin.
 
You can do this with JavaScript too, so the user won't have to go through submitting their form to get the error about it.

Code:
function Validator() {
   // get the text box's value
   var txt = document.formname.fieldname.value;

   // split into words
   var words = txt.split(" ");

   // count words
   if (words.length > 200) {
      window.alert ("Too many words!");
      return false;
   }

   return true;
}

........

<form name="formname" action="handler.cgi" onSubmit="Validator()">

<input type="text" name="fieldname">
<!-- or textarea or whatever it is -->
 
that way seens a lot easier and more straight forward. Thanks.
 
The only problem is it's very easy to bypass javascript, but you can't bypass the server script. You can use both server side and client side checking of the quantity of words, but you should not use only the javascript check.
 
High School seniors have very busy schedules. The way that they are doing the will and indexes now is by a simple form, there is a written word limit but nothing to check it. If somebody wants to waste their time trying to crack my code then for all means, go right ahead.
 
cracking javascript requires no skill or time, they simply need to disable javascript. You should be checking on the server side too, but it's up to you. I'm pretty sure Kirsle will agree with me.
 
what i'm saying is that it isn't something that is really needed. they weren't totally worried about it before and i'm sure they won't be now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top