Nathan McKaskle
Nathan McKaskle

Reputation: 3083

How do I write a regular expression that matches text that does not contain all caps?

I'm using the ASP.NET RegularExpressionValidator

I need a regular expression to keep users who fill out a form from using all caps.

For example, if they write their name:

Bob JONES or BOB JONES or BOB JOnes or whatever, it will not match.

I am able to match all caps with this regular expression:

[A-Z]{2,10}

But the RegularExpressionValidator requires me to match valid text, not invalid text.

Upvotes: 0

Views: 332

Answers (4)

Tim Cadieux
Tim Cadieux

Reputation: 455

Maybe i'm just stating the obvious, but couldn't you just to myVar.string.toLower before doing the Compare?

Upvotes: 0

Platinum Azure
Platinum Azure

Reputation: 46203

If your goal is to have each word have no more than 1 capital letter in a row at a time, and assuming it's okay to restrict to ASCII letters, try something like this:

^(?:[a-z]|[A-Z](?![A-Z])|['-])+$

In other words, the string must be entirely composed of either lowercase letters, or uppercase letters not followed by another uppercase letter.

This works for single words. For multiple words (like a full name, first and last), simply add a space to the alternation:

^(?:[a-z]|[A-Z](?![A-Z])|[\s'-])+$

(Edited to allow apostrophe and hyphen punctuation)

Upvotes: 2

burning_LEGION
burning_LEGION

Reputation: 13450

use this regular expression ^[a-z ]+$ if you want catch names like Bob Jones use this one ^([A-Z][a-z ]+)+$

Upvotes: 0

use this Regex: @"^[^A-Z]*$" It will match anything that not contains upper case characters.

Upvotes: 1

Related Questions