Is there a better way to do this? I cannot figure out how to add (?i)
so I can make the pattern globally case insensitive while still keeping the statement as a negation.
[Required(ErrorMessage = "Address")]
[RegularExpression("^(?!.*(p|P)\\.?(o|O)\\.?\\s+?(box|Box|BOX)).*$", ErrorMessage = "We cannot ship to PO boxes")]
public string CustomerAddress1 { get; set; }
I tested this out, and simply adding that
(?i)
to the beginning (as @sln says) works fine for me.Here's my test code, in a console app:
and here's the output:
EDIT: I apologize; I had only tried this out on the purely-C# end. With MVC model validation like it appears you're doing, you'd need a Regex expression that works in both C# and JavaScript. By nature, C# supports
(?i)...
to signify case-insensitivity, while JavaScript supports/.../i
. But neither notation will work in the other. Best you might be able to do is either what you already have (spelling outp|P
,o|O
, etc.), or else a customRegularExpressionWithOptions
attribute like Jeremy Cook's in the SO answer here.