I am using ASP.NET MVC 4 and I am looking for a attribute for Zip Code Validation
[Required(ErrorMessage = "Zip Code is Required")]
[ZipCode]
public string ZipCode { get; set; }
I know this doesn't work, but this is what I am looking for.
Can anyone Help
I need the Zip for just USA
You need to use a Regex. Try something like this.
[Required(ErrorMessage = "Zip is Required")]
[RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip")]
public string Zip { get; set; }
[Display(Name = "Zip Code")]
[StringLength(10, MinimumLength = 5)]
[RegularExpression("(^\\d{5}(-\\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} *\\d{1}[A-Z]{1}\\d{1}$)", ErrorMessage = "Zip code is invalid.")] // US or Canada
[Required(ErrorMessage = "Zip Code is Required.")]
public String ZipCode { set; get; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With