Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore case sensitivity in ASP.NET RegularExpressionValidator

Tags:

c#

regex

I have a RegularExpressionValidator where the only valid input is 8 characters long and consists of the letters MP followed by six digits. At the moment I have the following regex which does work

^(MP|mp|Mp|mP)[0-9]{6}$

but it feels a bit hacky. I'd like to be able to specify that the MP can be any combination of upper and lower case without having to list the available combinations.

Thanks,

David

like image 439
dlarkin77 Avatar asked Oct 23 '22 09:10

dlarkin77


1 Answers

You can do this when you define Regex object

Regex exp = new Regex(
    @"^mp[0-9]{6}$",
    RegexOptions.IgnoreCase);

Alternatively you can use ^(?i)mp[0-9]{6}$ syntax, which would make just specific bit of regex case-insensitive. But I would personally use the first option (it is easier to read).

For details see documentation on msnd.

like image 65
oleksii Avatar answered Nov 01 '22 18:11

oleksii