views:

576

answers:

2

I want to use an ASP.NET RegularExpressionValidator to limit the number of words in a text box. (The RegularExpressionValidator is my favoured solution because it will do both client and server side checks).

So what would be the correct Regex to put in the RegularExpressionValidator that will count the words and enforce a word-limit? For, lets say, 150 words.

(NB: I see that this question is similar, but the answers given seem to also rely on code such as Split() so I don't think any of them could plug into a RegularExpressionValidator which is why I'm asking again)

+3  A: 

Since ^ and $ is implicitly set with RegularExpressionValidators, use the following:

(\S*\s*){0,10}

The 0 here allows empty strings (more specifically 0 words) and 150 is the max number of words to accept. Adjust these as necessary to increase/decrease the number of words accepted.

The above regex is non-greedy, so you'll get a quicker match verses the one given in the question you reference. (\b.*\b){0,10} is greedy, so as you increased the number of words you'll see a decrease in performance.

Gavin Miller
The given regex won't allow symbols like question mark though
Paulo Manuel Santos
@pauloya - fixed
Gavin Miller
This works well in most situations. I've tried to test all the possible things that might come up ... I get a weird result with a string that is just over the limit and has unusual characters in it. For example: "one two{[]}!"£$%^#~@ three four five six seven-seven eight-eight nine ten eleven" and a ten-word limit. The validation still works but it takes about 30 seconds to run (on IE8 anyway), during which the whole browser freezes. Much longer strings with or without weird characters validate very quickly. Any idea why a case like the one above should takes so long?
codeulike
A: 

Check this site:

http://lawrence.ecorp.net/inet/samples/regexp-validate.php#count

its javascript regex, but is very similar to asp.net

its something like this:

(\b[a-z0-9]+\b.*){4,}

Gabriel Guimarães
that would set a lower limit, not an upper limit
James Deville
You shouldn't expect the complete code, thats just an example.
Gabriel Guimarães