rohit rohit - 1 year ago 81
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' ?

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download