regular expression limit characters

i would like a regular expression to validate a textbox that allows from 3 to 40 alphanumeric characters and it allows spaces anywhere in the string.

this is what ive got :  \w{3,40}
but it doesnt allow me to enter spaces...

thanks!
LVL 8
arcrossAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Richard QuadlingSenior Software DeveloperCommented:
Try ...


[ \w]{3,40}


That is [ and then a space and then \w]{3,40}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard QuadlingSenior Software DeveloperCommented:


[ \w]{3,40}

Options: case insensitive; ^ and $ match at line breaks

Match a single character present in the list below «[ \w]{3,40}»
   Between 3 and 40 times, as many times as possible, giving back as needed (greedy) «{3,40}»
   The character   « »
   A word character (letters, digits, etc.) «\w»


Created with RegexBuddy
0
Richard QuadlingSenior Software DeveloperCommented:
I would add ^ and $ as below to make sure the ENTIRE string is matched.

^[ \w]{3,40}$

Options: case insensitive; ^ and $ match at line breaks

Assert position at the beginning of a line (at beginning of the string or after a line break character) «^»
Match a single character present in the list below «[ \w]{3,40}»
   Between 3 and 40 times, as many times as possible, giving back as needed (greedy) «{3,40}»
   The character   « »
   A word character (letters, digits, etc.) «\w»
Assert position at the end of a line (at the end of the string or before a line break character) «$»


Created with RegexBuddy

0
arcrossAuthor Commented:
perfect!
thanks RQuadling!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.