Last active
December 31, 2015 01:28
-
-
Save Injac/7913944 to your computer and use it in GitHub Desktop.
Check Zip and postal codes (or whatever) based on ISO values and regular expressions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Validate zip code value | |
/// using ISO country code. | |
/// </summary> | |
public class ZipCodeValidator | |
{ | |
private Dictionary<string, string> countryList; | |
public Dictionary<string, string> CountryList | |
{ | |
get { return this.countryList; } | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="ZipCodeValidator" /> class. | |
/// </summary> | |
public ZipCodeValidator() | |
{ | |
this.countryList = new Dictionary<string, string>(); | |
this.InitCountryList(); | |
} | |
/// <summary> | |
/// Checks the zip code. | |
/// </summary> | |
/// <param name="countryCode">The country code.</param> | |
/// <param name="zipCode">The zip code.</param> | |
/// <returns></returns> | |
public bool CheckZipCode(string countryCode, string zipCode) | |
{ | |
if (countryCode.Length > 2 || countryCode.Length < 1 || !this.countryList.ContainsKey(countryCode.ToUpper())) | |
{ | |
throw new ArgumentException("Country Code not valid.", "countryCode"); | |
} | |
var matches = Regex.Matches(zipCode, this.countryList[countryCode.ToUpper()]); | |
if (matches.Count > 1 || matches.Count == 0) | |
{ | |
return false; | |
} | |
else if(matches.Count == 1) | |
{ | |
if (matches[0].Length < zipCode.Trim().Length) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
/// <summary> | |
/// Inits the country list. | |
/// </summary> | |
private void InitCountryList() | |
{ | |
#region CountryList | |
this.countryList.Add("AD", @"AD\d{3}"); | |
this.countryList.Add("AM", @"(37)?\d{4}"); | |
this.countryList.Add("AR", @"([A-HJ-NP-Z])?\d{4}([A-Z]{3})?"); | |
this.countryList.Add("AS", @"96799"); | |
this.countryList.Add("AT", @"\d{4}"); | |
this.countryList.Add("AU", @"\d{4}"); | |
this.countryList.Add("AX", @"22\d{3}"); | |
this.countryList.Add("AZ", @"\d{4}"); | |
this.countryList.Add("BA", @"\d{5}"); | |
this.countryList.Add("BB", @"(BB\d{5})?"); | |
this.countryList.Add("BD", @"\d{4}"); | |
this.countryList.Add("BE", @"\d{4}"); | |
this.countryList.Add("BG", @"\d{4}"); | |
this.countryList.Add("BH", @"(1[0-2]|[2-9])\d{2})?"); | |
this.countryList.Add("BM", @"[A-Z]{2}[ ]?[A-Z0-9]{2}"); | |
this.countryList.Add("BN", @"[A-Z]{2}[ ]?\d{4}"); | |
this.countryList.Add("BR", @"\d{5}[\-]?\d{3}"); | |
this.countryList.Add("BY", @"\d{6}"); | |
this.countryList.Add("CA", @"[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][ ]?\d[ABCEGHJ-NPRSTV-Z]\d"); | |
this.countryList.Add("CC", @"6799"); | |
this.countryList.Add("CH", @"\d{4}"); | |
this.countryList.Add("CK", @"\d{4}"); | |
this.countryList.Add("CL", @"\d{7}"); | |
this.countryList.Add("CN", @"\d{6}"); | |
this.countryList.Add("CR", @"\d{4,5}|\d{3}-\d{4}"); | |
this.countryList.Add("CS", @"\d{5}"); | |
this.countryList.Add("CV", @"\d{4}"); | |
this.countryList.Add("CX", @"6798"); | |
this.countryList.Add("CY", @"\d{4}"); | |
this.countryList.Add("CZ", @"\d{3}[ ]?\d{2}"); | |
this.countryList.Add("DE", @"\d{5}"); | |
this.countryList.Add("DK", @"\d{4}"); | |
this.countryList.Add("DO", @"\d{5}"); | |
this.countryList.Add("DZ", @"\d{5}"); | |
this.countryList.Add("EC", @"([A-Z]\d{4}[A-Z]|(?:[A-Z]{2})?\d{6})?"); | |
this.countryList.Add("EE", @"\d{5}"); | |
this.countryList.Add("EG", @"\d{5}"); | |
this.countryList.Add("ES", @"\d{5}"); | |
this.countryList.Add("ET", @"\d{4}"); | |
this.countryList.Add("FI", @"\d{5}"); | |
this.countryList.Add("FK", @"FIQQ 1ZZ"); | |
this.countryList.Add("FM", @"(9694[1-4])([ \-]\d{4})?"); | |
this.countryList.Add("FO", @"\d{3}"); | |
this.countryList.Add("FR", @"\d{2}[ ]?\d{3}"); | |
this.countryList.Add("GB", @"GIR[ ]?0AA|(AB|AL|B|BA|BB|BD|BH|BL|BN|BR|BS|BT|CA|CB|CF|CH|CM|CO|CR|CT|CV|CW|DA|DD|DE|DG|DH|DL|DN|DT|DY|E|EC|EH|EN|EX|FK|FY|G|GL|GY|GU|HA|HD|HG|HP|HR|HS|HU|HX|IG|IM|IP|IV|JE|KA|KT|KW|KY|L|LA|LD|LE|LL|LN|LS|LU|M|ME|MK|ML|N|NE|NG|NN|NP|NR|NW|OL|OX|PA|PE|PH|PL|PO|PR|RG|RH|RM|S|SA|SE|SG|SK|SL|SM|SN|SO|SP|SR|SS|ST|SW|SY|TA|TD|TF|TN|TQ|TR|TS|TW|UB|W|WA|WC|WD|WF|WN|WR|WS|WV|YO|ZE)(\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2})|BFPO[ ]?\d{1,4}"); | |
this.countryList.Add("GE", @"\d{4}"); | |
this.countryList.Add("GF", @"9[78]3\d{2}"); | |
this.countryList.Add("GG", @"GY\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}"); | |
this.countryList.Add("GL", @"39\d{2}"); | |
this.countryList.Add("GN", @"\d{3}"); | |
this.countryList.Add("GP", @"9[78][01]\d{2}"); | |
this.countryList.Add("GR", @"\d{3}[ ]?\d{2}"); | |
this.countryList.Add("GS", @"SIQQ 1ZZ"); | |
this.countryList.Add("GT", @"\d{5}"); | |
this.countryList.Add("GU", @"969[123]\d([ \-]\d{4})?"); | |
this.countryList.Add("GW", @"\d{4}"); | |
this.countryList.Add("HM", @"\d{4}"); | |
this.countryList.Add("HN", @"(?:\d{5})?"); | |
this.countryList.Add("HR", @"\d{5}"); | |
this.countryList.Add("HT", @"\d{4}"); | |
this.countryList.Add("HU", @"\d{4}"); | |
this.countryList.Add("ID", @"\d{5}"); | |
this.countryList.Add("IL", @"\d{5}"); | |
this.countryList.Add("IM", @"IM\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}"); | |
this.countryList.Add("IN", @"\d{6}"); | |
this.countryList.Add("IO", @"BBND 1ZZ"); | |
this.countryList.Add("IQ", @"\d{5}"); | |
this.countryList.Add("IS", @"\d{3}"); | |
this.countryList.Add("IT", @"\d{5}"); | |
this.countryList.Add("JE", @"JE\d[\dA-Z]?[ ]?\d[ABD-HJLN-UW-Z]{2}"); | |
this.countryList.Add("JO", @"\d{5}"); | |
this.countryList.Add("JP", @"\d{3}-\d{4}"); | |
this.countryList.Add("KE", @"\d{5}"); | |
this.countryList.Add("KG", @"\d{6}"); | |
this.countryList.Add("KH", @"\d{5}"); | |
this.countryList.Add("KR", @"\d{3}[\-]\d{3}"); | |
this.countryList.Add("KW", @"\d{5}"); | |
this.countryList.Add("KZ", @"\d{6}"); | |
this.countryList.Add("LA", @"\d{5}"); | |
this.countryList.Add("LB", @"(\d{4}([ ]?\d{4})?)?"); | |
this.countryList.Add("LI", @"(948[5-9])|(949[0-7])"); | |
this.countryList.Add("LK", @"\d{5}"); | |
this.countryList.Add("LR", @"\d{4}"); | |
this.countryList.Add("LS", @"\d{3}"); | |
this.countryList.Add("LT", @"\d{5}"); | |
this.countryList.Add("LU", @"\d{4}"); | |
this.countryList.Add("LV", @"\d{4}"); | |
this.countryList.Add("MA", @"\d{5}"); | |
this.countryList.Add("MC", @"980\d{2}"); | |
this.countryList.Add("MD", @"\d{4}"); | |
this.countryList.Add("ME", @"8\d{4}"); | |
this.countryList.Add("MG", @"\d{3}"); | |
this.countryList.Add("MH", @"969[67]\d([ \-]\d{4})?"); | |
this.countryList.Add("MK", @"\d{4}"); | |
this.countryList.Add("MN", @"\d{6}"); | |
this.countryList.Add("MP", @"9695[012]([ \-]\d{4})?"); | |
this.countryList.Add("MQ", @"9[78]2\d{2}"); | |
this.countryList.Add("MT", @"[A-Z]{3}[ ]?\d{2,4}"); | |
this.countryList.Add("MU", @"(\d{3}[A-Z]{2}\d{3})?"); | |
this.countryList.Add("MV", @"\d{5}"); | |
this.countryList.Add("MX", @"\d{5}"); | |
this.countryList.Add("MY", @"\d{5}"); | |
this.countryList.Add("NC", @"988\d{2}"); | |
this.countryList.Add("NE", @"\d{4}"); | |
this.countryList.Add("NF", @"2899"); | |
this.countryList.Add("NG", @"(\d{6})?"); | |
this.countryList.Add("NI", @"(\d{4}-)?\d{3}-\d{3}(-\d{1})?)?"); | |
this.countryList.Add("NL", @"\d{4}[ ]?[A-Z]{2}"); | |
this.countryList.Add("NO", @"\d{4}"); | |
this.countryList.Add("NP", @"\d{5}"); | |
this.countryList.Add("NZ", @"\d{4}"); | |
this.countryList.Add("OM", @"(PC )?\d{3}"); | |
this.countryList.Add("PF", @"987\d{2}"); | |
this.countryList.Add("PG", @"\d{3}"); | |
this.countryList.Add("PH", @"\d{4}"); | |
this.countryList.Add("PK", @"\d{5}"); | |
this.countryList.Add("PL", @"\d{2}-\d{3}"); | |
this.countryList.Add("PM", @"9[78]5\d{2}"); | |
this.countryList.Add("PN", @"PCRN 1ZZ"); | |
this.countryList.Add("PR", @"00[679]\d{2}([ \-]\d{4})?"); | |
this.countryList.Add("PT", @"\d{4}([\-]\d{3})?"); | |
this.countryList.Add("PW", @"96940"); | |
this.countryList.Add("PY", @"\d{4}"); | |
this.countryList.Add("RE", @"9[78]4\d{2}"); | |
this.countryList.Add("RO", @"\d{6}"); | |
this.countryList.Add("RS", @"\d{6}"); | |
this.countryList.Add("RU", @"\d{6}"); | |
this.countryList.Add("SA", @"\d{5}"); | |
this.countryList.Add("SE", @"\d{3}[ ]?\d{2}"); | |
this.countryList.Add("SG", @"\d{6}"); | |
this.countryList.Add("SH", @"(ASCN|STHL) 1ZZ"); | |
this.countryList.Add("SI", @"\d{4}"); | |
this.countryList.Add("SJ", @"\d{4}"); | |
this.countryList.Add("SK", @"\d{3}[ ]?\d{2}"); | |
this.countryList.Add("SM", @"4789\d"); | |
this.countryList.Add("SN", @"\d{5}"); | |
this.countryList.Add("SO", @"\d{5}"); | |
this.countryList.Add("SZ", @"[HLMS]\d{3}"); | |
this.countryList.Add("TC", @"TKCA 1ZZ"); | |
this.countryList.Add("TH", @"\d{5}"); | |
this.countryList.Add("TJ", @"\d{6}"); | |
this.countryList.Add("TM", @"\d{6}"); | |
this.countryList.Add("TN", @"\d{4}"); | |
this.countryList.Add("TR", @"\d{5}"); | |
this.countryList.Add("TW", @"\d{3}(\d{2})?"); | |
this.countryList.Add("UA", @"\d{5}"); | |
this.countryList.Add("US", @"\d{5}([ \-]\d{4})?"); | |
this.countryList.Add("UY", @"\d{5}"); | |
this.countryList.Add("UZ", @"\d{6}"); | |
this.countryList.Add("VA", @"00120"); | |
this.countryList.Add("VE", @"\d{4}"); | |
this.countryList.Add("VI", @"008([0-4]\d)|(5[01]))([ \-]\d{4})?"); | |
this.countryList.Add("WF", @"986\d{2}"); | |
this.countryList.Add("XK", @"\d{5}"); | |
this.countryList.Add("YT", @"976\d{2}"); | |
this.countryList.Add("YU", @"\d{5}"); | |
this.countryList.Add("ZA", @"\d{4}"); | |
this.countryList.Add("ZM", @"\d{5}"); | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment