I’m using MVC3 and need to create a RegularExpression attribute for a property in a model to validate that the user has not entered brackets. Anyone know what this regex string would look like?
This is what I have right now.
[Required]
[RegularExpression("--enter regex here--", ErrorMessage = "You cannot use '[' or ']' on the title ")]
public string Title { get; set; }
You can use a negated character class with the characters that you don’t want.
Note that square brackets are considered to be “special” in regex, so you’ll need to escape them like so: