views:

164

answers:

2

I am using a regular expression to limit words entered in a textbox field to 250-500 words.

(((^\s*)*\S+\s+)|(\S+)){250,500}

Since I know little to nothing about regular expressions, I had copied it from another website. I get the validation error regardless of how many words are entered.

Here is the page that the form is on, if you wish to try it for yourself:

http://mlknew.timpecoraro.com/dreamers-community/win-a-dream/

The site is running Wordpress with the cforms plugin, thus I can only use regular expressions for validation.

Thanks for any help!

A: 

why don't you try

(\w+\s+){250,500}

using str-word-count would be better if your wordpress hosting have php version superior to 4.3 .

RageZ
No, that will match exactly 250 consecutive characters in the "word" class.
Adam Bellaire
right why don't you split using \s+ regexp and count the number of result ?
RageZ
like$array = preg_split('/\s+/', $subject);if(count($array)< 250 || count($array)>500){ echo "bad";}
RageZ
+1  A: 

How about

^\s*(\S+\s+){250,499}\S*$

This will first anchor the string to be searched at beginning and end of the string. Then optionally match whitespace (in case the string begins with some). Then match 249-499 repeats of (one or more non-space characters)+(one or more space characters, including linebreaks). Then match an optional chunk of non-space characters in order to get to 500 (but also not fail if the text contains whitespace at the end).

Tim Pietzcker
Still getting the validation errors, regardless of the number of words entered. It may be a problem other than the specific regex entered. . . .
kdietz
Figured it out . . . the plugin was stripping out the slashes. Good regex though!
kdietz