Forum Moderators: coopster & phranque

Message Too Old, No Replies

how to shorten textarea input to 250 words

Limit maximum number of words submitted in textarea

         

cla313

9:36 pm on Dec 6, 2006 (gmt 0)

10+ Year Member



Hi. I would like to shorten the text submitted by users using a textarea field in one of my forms and I would like to do this with perl, because I am using nms formmail to process the form data.

On the client side I have a javascript showing the characters usage as the user types in, while on the server side I modified the perl script so that it chops off all characters after the number 1500.

unfortunately, I was asked to do put the maximum allowance on words instead of characters; but counting the words in the string passed to the script and chopping all text coming after word n. 250 is more difficult than I thought. I googled to see if I could find some info on how to do this but had no success.

can somebody help please?
thanks in advance!

Moby_Dim

4:26 pm on Dec 7, 2006 (gmt 0)

10+ Year Member



May be the most simple and understandable is something like :

my($counter) = 0;
my($result) = '';

while($in{'my_textarea_form_input'} =~ m/(\w+\W+(?=\w))/g and $counter++ <= 250) {
$result .= $1
}

[edited by: Moby_Dim at 4:28 pm (utc) on Dec. 7, 2006]

Moby_Dim

8:34 pm on Dec 7, 2006 (gmt 0)

10+ Year Member



I've tested, and it's work but at the same time have found the more elegant solution :

$in{'text_area'} =~ m/((\w+\W+(?=\w)){250})/;

The text needed (here - the first 250 words chunk) is in $1.

The text_area may contain new line characters. s quantifier may be added then may be (have not tested this.) Also you have to count that the number of words may be less than 250. In this case the prev.post code is more flexible.

perl_diver

8:39 pm on Dec 7, 2006 (gmt 0)

10+ Year Member



just change it to:

$in{'text_area'} =~ m/((\w+\W+(?=\w)){1,250})/;

and it will get whatever is available up to 250 "words"

cla313

4:49 pm on Dec 19, 2006 (gmt 0)

10+ Year Member



I know it's late, but better late than never: thanks everybody for the help!