2

I'm trying to add validation to a form field so that if it contains a certain word, an error is displayed. At the moment I have this:

Validation.Add("field-1", Validator.Regex("^((?!TEST)(?!test).)*$", "Field may not contain 'test'"));

This works fine for "TEST" and "test", but won't prevent someone entering "tESt".

I've tried adding the case insensitive flag, but this made the regex stop working completely:

Validation.Add("field-1", Validator.Regex("/^((?!TEST)(?!test).)*$/i", "Field may not contain 'test'"));

I also read here that (?i) can be used to ignore case, but that didn't work either - maybe I'm putting it in the wrong place:

Validation.Add("field-1", Validator.Regex("^((?i)(?!TEST)(?!test).)*$", "Field may not contain 'test'"));

Is this doable without adding every possible variation of "test"?

Community
  • 1
  • 1
Andy
  • 364
  • 1
  • 8
  • 21
  • 1
    Try `"(?i)^(?!.*TEST).*$"`. It also seems your [`"^((?i)(?!TEST)(?!test).)*$"`](http://regexstorm.net/tester?p=%5e%28%28%3fi%29%28%3f!TEST%29%28%3f!test%29.%29*%24&i=There+is+a+tESt+here) should also work (although a tempered greedy token here is an "overkill"). – Wiktor Stribiżew Feb 27 '17 at 13:56
  • No luck with that. Seems like any variation of (?i) kills the whole thing. – Andy Feb 27 '17 at 14:13
  • 1
    Does it mean you run this on the client side? `(?i)` won't work in JS, it will only work in .NET (well, and in some other regex flavors). – Wiktor Stribiżew Feb 27 '17 at 14:14
  • I'm not entirely sure how this works, but it seems to be client side using both .NET and JS. The validation rule adds a bunch of data attributes to my form field, including this: `data-val-regex-pattern="(?i)^(?!.*TEST).*$"`. I don't know what's going on behind the scenes, but it seems like the JS part doesnt like (?i) and the .NET part doesnt like the /i flag. – Andy Feb 27 '17 at 14:32
  • 1
    Right, JS will never understand `(?i)`. You have to use `^(?!.*[Tt][Ee][sS][tT]).*$` – Wiktor Stribiżew Feb 27 '17 at 14:44
  • Thanks, you're a saint. Can't believe I didn't think of this. Can you post it as an answer so I can mark it as accepted? – Andy Feb 27 '17 at 14:55
  • 1
    Posted with some explanations. – Wiktor Stribiżew Feb 27 '17 at 15:01

2 Answers2

4

Use an instance of the Regex class with a RegexOptions.IgnoreCase parameter.

Regex validator = new Regex(@"TEST", RegexOptions.IgnoreCase);

if(validator.IsMatch(formValue))
{
    // Do something about this
}
Serge
  • 3,986
  • 2
  • 17
  • 37
1

It appears that that your regex is handled on the client side. JavaScript RegExp does not support (?i) inline modifier (it does not support any inline modifiers, even the XRegExp library), so the only way out is to spell out all the letter cases using character classes:

Validation.Add("field-1", Validator.Regex("^(?!.*[Tt][Ee][sS][tT]).*$", "Field may not contain 'test'"));

Note that your tempered greedy token is too resource consuming a pattern, it is easier to use a simple negative lookahead here. If you need to support multiline strings, replace the . with [\s\S] character class.

Details:

  • ^ - start of string
  • (?!.*[Tt][Ee][sS][tT]) - there cannot be any Test or TesT, etc., after any 0+ chars
  • .* - any 0+ chars, as many as possible
  • $ - end of string.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563