Use the RegularExpressionValidator parameter to limit the number of words?

I want to use ASP.NET RegularExpressionValidator to limit the number of words in a text field. (Regular ExpressionValidator is my approved solution because it will check both client side and server side).

So, what would be the correct Regex to insert into a RegularExpressionValidator that will count words and apply a word limit? Say 150 words.

(NB: I see this question is similar, but the answers seem to also depend on code such as Split (), so I don't think that they can connect to RegularExpressionValidator, so I ask again)

+4
source share
3 answers

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

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

Here 0 allows empty lines (more specifically 0 words), and 150 - the maximum number of words to accept. Adjust them if necessary to increase / decrease the number of received words.

The above regex is not greedy, so you get a faster match for the verses mentioned in the question you are referencing. (\b.*\b){0,10} greedy, because you have increased the number of words, you will see a decrease in performance.

+5
source

Here is a short link for regular expressions: http://msdn.microsoft.com/en-us/library/az24scfc.aspx

You can use this site to check expressions: http://regexpal.com/

Here is an example of my regular expression that works with both the minimum and maximum number of words (and corrects the error with the leading interval):

 ^\s*(\S+\s+|\S+$){10,150}$ 
+1
source

Check out this site:

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

its javascript regex but very similar to asp.net

something like that:

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

0
source

All Articles