Link to home
Create AccountLog in
Avatar of devgirl1978
devgirl1978

asked on

Regular Expression Validator - limit number of words

Hi experts,

I'd like to use a .net regular expression validator to limit the number of words in a textbox to 15.

My research has come up with this: "\b(\w+?)\b{,15}" But it returns false for any string I enter!

Please help me find a regular expression that will do what I want?

Thanks in advance.
Avatar of Maulik Modi
Maulik Modi

Can you try, \b(\w+?)\b{1,15}" ....?
Avatar of devgirl1978

ASKER

That gives me a Javascript error: "unexpected quantifier"
your syntax isn't correct on selecting a word - you should be looking for
\b\w*\S\b
i.e. the beginning of a word, any number of characters that aren't whitespace, then the end of the word. this will give you all the words in a textbox.
try
(\b\w*\S\b){15}
that should be a word repeated exactly 15 times
Thanks but when I tried that regular expression, it does not allowing anything to pass... and I need to allow up to 15 words, not exactly 15 words.
can't u use the maxlength property of the textbox to 15 for this ... if u still want to regular expressions then

use these in the regularexpressionvalidator
to validate only alphabets(in either case)  - minimum length 4 & maximum length 32. Only white spaces are allowed apart from alphabets.
^([a-zA-z\s]{4,32})$

to validate max 15 characters
^.{0,15}$

Rejohny - thanks for your input but I need to validate max 15 words in the textbox, not characters...
Avatar of Jesse Houwing
^([A-Za-z]+\b)(\s*[A-Za-z]+\b){14}$

will allow only 15 words. If you need to allow a '.', ',' or other punctiuation it get's harder, but something like this will probably do:

^([A-Za-z]+\b)(\s*[-.,'";:|?`!@#$%^&*()_=+\[]{}<>]*\s*[-.,'";:|?`!@#$%^&*()_=+\[]{}<>]*[A-Za-z]+\b){14}\s*[-.,'";:|?`!@#$%^&*()_=+\[]{}<>]*$

though this might need some tweaking ;)

Note that \w also allows numbers and the '_'. Which is probably not what you wanted.

Also note that checking for \b is only needed to the end of each word, as this automatically prevents two words from being linked to eachother.


It might be faster though to just checklike this:

^[a-zA-Z ]+$

And then split the string in the space and check if you get an array of at most 15 strings as a result.
ToAoM: neither of those expressions worked... :( Thanks for trying

What I really need is for this to work specifically in an ASP.NET validator, so I need any suggested regular expressions to be tested in a validator control (as this type of control seems to be a bit fussy).

The validator control is validating a textarea where people will write various descriptions, so all punctuation, numbers, etc must be allowed. In fact the validation should just be "up to 15 groups of characters separated by a space", as opposed to "up to 15 words".

Any help much appreciated!
SOLUTION
Avatar of Jesse Houwing
Jesse Houwing
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Okay so what I ended up doing was writing my own custom validator (extending BaseValidator) that takes in a parameter for max. number of words. It uses a client side Javascript function to split the value on " " and count the words from there.

I split the points between Rejojohny and ToAoM because both helped me towards the solution.