rohit rohit - 3 months ago 13
ASP.NET (C#) Question

Regex to allow only string but not word "test"

is the original
used for firstname field in my project. I want to modify this
such that it will not allow 'test' As an input.

works fine to exclude word 'test' but it allows spaces & numbers. so what will be the regex which will allow only
as an input but will not allow word 'test' i.e. first name may not be 'test' ?


You may use


See the regex demo.


  • ^ - start of string
  • (?!test$) - the string cannot equal test
  • [a-zA-Z'.\s]{2,40} - 2 to 40 symbols that are ASCII letters, ', . or whitespace
  • $ - end of string