Regex data annotation making model state invalid ASP.NET Core

532 Views Asked by At

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.

1

There are 1 best solutions below

6
On BEST ANSWER

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")]