Search code examples
c#asp.net-mvcdata-annotations

ASP.NET Data Annotations Regular Expression: Don't allow following characters in Name: \, /, :, *, ;,


As the title says, I'm looking for a regular expression that don't allow a user to submit the Name with characters : (colon), ; (semicolon), / (forward-slash), \ (backward-slash), * (asterisk) and . (dot)

[Required]
public string Name { get; set; }

Solution

  • I finally ended up with this solution thanks to great suggestions from people on this thread:

    [Required]
    [RegularExpression(@"^[^\\/:*;\.\)\(]+$", ErrorMessage = "The characters ':', '.' ';', '*', '/' and '\' are not allowed")]
    public string Name { get; set; }