C#- Validation for US or Canadian zip code

前端 未结 3 742
旧时难觅i
旧时难觅i 2020-12-14 18:11

I am using following method to validate US or Canadian zip code, but i think it is not working fine for me. Please suggest me the changes in the regular expression.

3条回答
  •  北海茫月
    2020-12-14 18:35

    If you are using Data Annotation Validators, you can use a RegularExpression attribute like this:

    [RegularExpression(@"(^\d{5}(-\d{4})?$)|(^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[ABCEGHJKLMNPRSTVWXYZabceghjklmnprstv‌​xy]{1} *\d{1}[ABCEGHJKLMNPRSTVWXYZabceghjklmnprstvxy]{1}\d{1}$)", ErrorMessage = "That postal code is not a valid US or Canadian postal code.")]
    

    (regex is from the link @huMptyduMpty posted above at http://geekswithblogs.net/MainaD/archive/2007/12/03/117321.aspx but my regex allows both upper and lower case letters)

提交回复
热议问题