PixelPaul
PixelPaul

Reputation: 2767

asp.net mvc model validation with value match

I would like to check user input of a zip code field to reject any input that isn't one of 3 specified values:

53119 53029 53214

Is it possible to do this with the built in model validation, or will I have to write something custom?

Upvotes: 2

Views: 262

Answers (1)

Mikael Hardø
Mikael Hardø

Reputation: 228

I would propose that you wrote something custom if there is any chance that this list will change in the future, but it is indeed possible doing something like this:

public class UserModel
{
    [Required(ErrorMessage = "Please Enter Zip")]
    [Display(Name = "Email")]
    [RegularExpression(@"^(53119|53029|53214)", ErrorMessage = "Please Enter Valid Zip")]
    public string ZipCode{ get; set; } 
}

Upvotes: 3

Related Questions