Make regular expression case insensitive in ASP.NET RegularExpressionValidator

Given this regular expression: "^[0-9]*\s*(lbs|kg|kgs)$" how do I make it case insensitive? I am trying to use this in a .net regular expression validator, so I need to specify case insensitivity in the pattern.

I can not use the RegexOptions programatically because I am specifying the regular expression in a RegularExpressionValidator


Solution 1:

I found out.

Case sensitive: ^[0-9]\s(lbs|kg|kgs)$

Case insensitive: (?i:^[0-9]\s(lbs|kg|kgs)$)

I believe that this is specific to the .NET implementation of regular expressions. So if you use this in the RegularExpressionValidator you have to turn off client side validation because the javascript regex parser will not recognize the ?i token.

Solution 2:

Use RegEx Options.

Regex regExInsensitive = new Regex(@"^[0-9]\s(lbs|kg|kgs)$", RegexOptions.IgnoreCase);

In other languages you can usually specify a RegEx modifier after the end of the Reg Ex; the 'case insensitive' modifier is 'i':

In Perl:

if($var =~ /^[0-9]\s(lbs|kg|kgs)$/i) { # the /i means case insensitive
    # ...
}

In PHP:

if(preg_match("/^[0-9]\s(lbs|kg|kgs)$/i", $var)) {
    // ...
}

Solution 3:

Can we make Regex case-insensitive in C#? : Yes

Set option inline via (?i) pattern construct or via RegexOptions.IgnoreCase param

Can we make Regex case-insensitive in JavaScript? : Yes

Set flag via /pattern/flags syntax or the insensitive flag /REGEX/i

(Aside) Can we make Regex case-insensitive in HTML5 Pattern Attribute? : No

As Chris R. Timmons points out on RegularExpressionValidator doesn't ignore case:

There is no property in the RegularExpressionValidator control that allows regex options to be set.

If your control does both client-side and server-side validation, the regex must use a subset of regular expression syntax that both JS and .NET can execute. In this case, to make a regex ignore case it is necessary to use a character class construct like [a-zA-Z] to match both upper and lower case characters.

If your validation is done on the server-side only, you can use the more powerful .NET regular expression syntax. In this case, you can place the (?i) option at the beginning of the regex to tell it to ignore case.

So, if you want to use the out of the box validator, you're stuck with Geoff's solution of using character sets like this: [aA]

Solution 4:

Here is an alternative using a CustomValidator, when really needing case-insensitivity server-side and client-side; and the the Upper/lower [A-Za-z] char approach is too much.

This blends the various other answers, using the server-side RegEx object and client-side JavaScript syntax.

CustomValidator:

<asp:CustomValidator ID="cvWeight" runat="server" ControlToValidate="txtWeight"
  OnServerValidate="cvWeight_Validate" ClientValidationFunction="cvWeight_Validate"
  ValidateEmptyText="true" Text="*" ErrorMessage="Invalid entry." />

Code behind:

protected void cvWeight_Validate(object sender, ServerValidateEventArgs args)
{
    Regex re = new Regex(@"^[0-9]*\s*(lbs|kg|kgs)$", RegexOptions.IgnoreCase);
    args.IsValid = re.IsMatch(args.Value);
}

Client-side validation function:

function cvWeight_Validate(sender, args) {
  var reWeight = /^[0-9]*\s*(lbs|kg|kgs)$/i;
  args.IsValid = reWeight.test(args);
}

This is working fine for me, except when using a ValidationSummary. On the client-side validation, the error * shows, but I can't get the error message to display in the summary. The summary only displays when submitted. I think it's supposed to display; I have a mix of update panels and legacy code, which may be problems.