ASP.NET MVC 4 Postcode Check

I am using ASP.NET MVC 4 and I am looking for an attribute to check the zip code

[Required(ErrorMessage = "Zip Code is Required")] [ZipCode] public string ZipCode { get; set; } 

I know this does not work, but that is what I am looking for.

Can anyone help

I need a zip for just usa

+6
source share
2 answers

You need to use Regex. Try something like this.

 [Required(ErrorMessage = "Zip is Required")] [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip")] public string Zip { get; set; } 
+17
source
 [Display(Name = "Zip Code")] [StringLength(10, MinimumLength = 5)] [RegularExpression("(^\\d{5}(-\\d{4})?$)|(^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[AZ]{1} *\\d{1}[AZ]{1}\\d{1}$)", ErrorMessage = "Zip code is invalid.")] // US or Canada [Required(ErrorMessage = "Zip Code is Required.")] public String ZipCode { set; get; } 
+1
source

Source: https://habr.com/ru/post/945508/


All Articles