kdietz
kdietz

Reputation: 3

Trouble with a word limit range regular expression

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!

Upvotes: 0

Views: 1759

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336408

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).

Upvotes: 2

RageZ
RageZ

Reputation: 27323

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 .

Upvotes: 0

Related Questions