Johnston
Johnston

Reputation: 2873

Checking if first character is Uppercase regex

I have created a regex that only allows the user to enter numbers and letters.

^[a-zA-Z0-9]+$

I wanted to make sure that the first character is uppercase so I looked up how to do it and i changed my equation to this:

^[A-Z][a-zA-Z0-9]+$

I tested my equation on a few sites that test regex and it works properly but when I put it into my program it doesnt seem to work.

Upvotes: 2

Views: 8665

Answers (2)

Holistic Developer
Holistic Developer

Reputation: 2526

Use a Regex method that allows you to specify a RegexOptions value, with RegexOptions.IgnoreCase turned off.

Upvotes: 2

JKor
JKor

Reputation: 3832

Change ^[A-Z][a-zA-Z0-9]+$ to ^[A-Z][a-zA-Z0-9]*$. If it is ^[A-Z][a-zA-Z0-9]+$ then you are requiring a 2 character string, the first letter of which is capital. The modified version allows 1 character strings as well. Does that help?

Upvotes: 5

Related Questions