Search code examples
c#regexasp.net-coredata-annotationsmodelstate

Regex data annotation making model state invalid ASP.NET Core


I have a simple regex data annotation

[RegularExpression(@"\d{5}", ErrorMessage = "Zipcode must be exactly 5 digits")]
public string Zipcode
{
    get;set;
}

When the value of the Zipcode is "12345"

Model.IsValid returns false.


Solution

  • Try this it is used for validation Zip code. The Zip code should contain 5 digits and should not be 00000. [RegularExpression(@"^(?!00000)[0-9]{5,5}$", ErrorMessage = "Zip code should contain 5 digits")]