Ignore case sensitivity in ASP.NET RegularExpressionValidator

I have a RegularExpressionValidator where the only valid input is 8 characters and consists of the letters MP followed by six digits. At the moment, I have the following regex that works

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

but it feels a bit hacked. I would like to be able to indicate that MP can be any combination of upper and lower case without having to list the available combinations.

Thanks,

David

+4
source share
1 answer

You can do this when you define a regex object

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

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

See the msnd documentation for more details .

+3
source

Source: https://habr.com/ru/post/1396155/


All Articles