Erik
Erik

Reputation: 137

ASP.NET Regular Expression Validator

I need a Regular Expression that can validate an exact 3 character(alpha only) code but also a blank field to set as the validation expression of a ASP.NET RegEx validator control. I am currently using ^[a-zA-Z]{3}$ and this works out well to match the code but of course doesn't match a blank. I have been looking at using something like this: ^(?:|)[a-zA-Z]{3}$

Upvotes: 2

Views: 1241

Answers (2)

rkw
rkw

Reputation: 7297

Have you tried using (^$)|(^[a-zA-Z]{3}$)?

Upvotes: 0

Ahmad Mageed
Ahmad Mageed

Reputation: 96557

If your intention is to allow blank fields, then use the original pattern of ^[a-zA-Z]{3}$ since the RegularExpressionValidator doesn't validate blank fields. It will allow them.

However, if you want to prevent blank entries then you'll need to add a RequiredFieldValidator to validate the same control, in addition to the RegularExpressionValidator.

Upvotes: 8

Related Questions