Created
August 22, 2014 15:11
-
-
Save intel352/df12f16788a91b51c60f to your computer and use it in GitHub Desktop.
Postal code validation
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
| importPackage( dw.system ); | |
| importScript("utils/Underscore.ds"); | |
| var _ = getUnderscore(); | |
| function PostalCode() {} | |
| (function(){ | |
| // Sourced from http://unicode.org/cldr/trac/browser/tags/release-24/common/supplemental/postalCodeData.xml | |
| PostalCode.patterns = [ | |
| { | |
| "territoryId": "GB", | |
| "regex": "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}" | |
| }, | |
| { | |
| "territoryId": "JE", | |
| "regex": "JE\\d[\\dA-Z]?[ ]?\\d[ABD-HJLN-UW-Z]{2}" | |
| }, | |
| { | |
| "territoryId": "GG", | |
| "regex": "GY\\d[\\dA-Z]?[ ]?\\d[ABD-HJLN-UW-Z]{2}" | |
| }, | |
| { | |
| "territoryId": "IM", | |
| "regex": "IM\\d[\\dA-Z]?[ ]?\\d[ABD-HJLN-UW-Z]{2}" | |
| }, | |
| { | |
| "territoryId": "US", | |
| "regex": "\\d{5}(?:[ \\-]\\d{4})?" | |
| }, | |
| { | |
| "territoryId": "CA", | |
| "regex": "[ABCEGHJKLMNPRSTVXY]\\d[ABCEGHJ-NPRSTV-Z][ ]?\\d[ABCEGHJ-NPRSTV-Z]\\d" | |
| }, | |
| { | |
| "territoryId": "DE", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "JP", | |
| "regex": "\\d{3}-\\d{4}" | |
| }, | |
| { | |
| "territoryId": "FR", | |
| "regex": "\\d{2}[ ]?\\d{3}" | |
| }, | |
| { | |
| "territoryId": "AU", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "IT", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "CH", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "AT", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "ES", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "NL", | |
| "regex": "\\d{4}[ ]?[A-Z]{2}" | |
| }, | |
| { | |
| "territoryId": "BE", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "DK", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "SE", | |
| "regex": "\\d{3}[ ]?\\d{2}" | |
| }, | |
| { | |
| "territoryId": "NO", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "BR", | |
| "regex": "\\d{5}[\\-]?\\d{3}" | |
| }, | |
| { | |
| "territoryId": "PT", | |
| "regex": "\\d{4}([\\-]\\d{3})?" | |
| }, | |
| { | |
| "territoryId": "FI", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "AX", | |
| "regex": "22\\d{3}" | |
| }, | |
| { | |
| "territoryId": "KR", | |
| "regex": "\\d{3}[\\-]\\d{3}" | |
| }, | |
| { | |
| "territoryId": "CN", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "TW", | |
| "regex": "\\d{3}(?:\\d{2})?" | |
| }, | |
| { | |
| "territoryId": "SG", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "DZ", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "AD", | |
| "regex": "AD\\d{3}" | |
| }, | |
| { | |
| "territoryId": "AR", | |
| "regex": "(?:[A-HJ-NP-Z])?\\d{4}(?:[A-Z]{3})?" | |
| }, | |
| { | |
| "territoryId": "AM", | |
| "regex": "(?:37)?\\d{4}" | |
| }, | |
| { | |
| "territoryId": "AZ", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "BH", | |
| "regex": "(?:(?:1[0-2]|[2-9])\\d{2})?" | |
| }, | |
| { | |
| "territoryId": "BD", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "BB", | |
| "regex": "(?:BB\\d{5})?" | |
| }, | |
| { | |
| "territoryId": "BY", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "BM", | |
| "regex": "[A-Z]{2}[ ]?[A-Z0-9]{2}" | |
| }, | |
| { | |
| "territoryId": "BA", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "IO", | |
| "regex": "BBND 1ZZ" | |
| }, | |
| { | |
| "territoryId": "BN", | |
| "regex": "[A-Z]{2}[ ]?\\d{4}" | |
| }, | |
| { | |
| "territoryId": "BG", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "KH", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "CV", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "CL", | |
| "regex": "\\d{7}" | |
| }, | |
| { | |
| "territoryId": "CR", | |
| "regex": "\\d{4,5}|\\d{3}-\\d{4}" | |
| }, | |
| { | |
| "territoryId": "HR", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "CY", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "CZ", | |
| "regex": "\\d{3}[ ]?\\d{2}" | |
| }, | |
| { | |
| "territoryId": "DO", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "EC", | |
| "regex": "(?:[A-Z]\\d{4}[A-Z]|(?:[A-Z]{2})?\\d{6})?" | |
| }, | |
| { | |
| "territoryId": "EG", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "EE", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "FO", | |
| "regex": "\\d{3}" | |
| }, | |
| { | |
| "territoryId": "GE", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "GR", | |
| "regex": "\\d{3}[ ]?\\d{2}" | |
| }, | |
| { | |
| "territoryId": "GL", | |
| "regex": "39\\d{2}" | |
| }, | |
| { | |
| "territoryId": "GT", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "HT", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "HN", | |
| "regex": "(?:\\d{5})?" | |
| }, | |
| { | |
| "territoryId": "HU", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "IS", | |
| "regex": "\\d{3}" | |
| }, | |
| { | |
| "territoryId": "IN", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "ID", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "IL", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "JO", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "KZ", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "KE", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "KW", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "LA", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "LV", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "LB", | |
| "regex": "(?:\\d{4}(?:[ ]?\\d{4})?)?" | |
| }, | |
| { | |
| "territoryId": "LI", | |
| "regex": "(?:948[5-9])|(?:949[0-7])" | |
| }, | |
| { | |
| "territoryId": "LT", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "LU", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "MK", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "MY", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "MV", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "MT", | |
| "regex": "[A-Z]{3}[ ]?\\d{2,4}" | |
| }, | |
| { | |
| "territoryId": "MU", | |
| "regex": "(?:\\d{3}[A-Z]{2}\\d{3})?" | |
| }, | |
| { | |
| "territoryId": "MX", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "MD", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "MC", | |
| "regex": "980\\d{2}" | |
| }, | |
| { | |
| "territoryId": "MA", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "NP", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "NZ", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "NI", | |
| "regex": "(?:(?:\\d{4}-)?\\d{3}-\\d{3}(?:-\\d{1})?)?" | |
| }, | |
| { | |
| "territoryId": "NG", | |
| "regex": "(?:\\d{6})?" | |
| }, | |
| { | |
| "territoryId": "OM", | |
| "regex": "(?:PC )?\\d{3}" | |
| }, | |
| { | |
| "territoryId": "PK", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "PY", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "PH", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "PL", | |
| "regex": "\\d{2}-\\d{3}" | |
| }, | |
| { | |
| "territoryId": "PR", | |
| "regex": "00[679]\\d{2}(?:[ \\-]\\d{4})?" | |
| }, | |
| { | |
| "territoryId": "RO", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "RU", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "SM", | |
| "regex": "4789\\d" | |
| }, | |
| { | |
| "territoryId": "SA", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "SN", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "SK", | |
| "regex": "\\d{3}[ ]?\\d{2}" | |
| }, | |
| { | |
| "territoryId": "SI", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "ZA", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "LK", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "TJ", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "TH", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "TN", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "TR", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "TM", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "UA", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "UY", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "UZ", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "VA", | |
| "regex": "00120" | |
| }, | |
| { | |
| "territoryId": "VE", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "ZM", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "AS", | |
| "regex": "96799" | |
| }, | |
| { | |
| "territoryId": "CC", | |
| "regex": "6799" | |
| }, | |
| { | |
| "territoryId": "CK", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "RS", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "ME", | |
| "regex": "8\\d{4}" | |
| }, | |
| { | |
| "territoryId": "CS", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "YU", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "CX", | |
| "regex": "6798" | |
| }, | |
| { | |
| "territoryId": "ET", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "FK", | |
| "regex": "FIQQ 1ZZ" | |
| }, | |
| { | |
| "territoryId": "NF", | |
| "regex": "2899" | |
| }, | |
| { | |
| "territoryId": "FM", | |
| "regex": "(?:9694[1-4])(?:[ \\-]\\d{4})?" | |
| }, | |
| { | |
| "territoryId": "GF", | |
| "regex": "9[78]3\\d{2}" | |
| }, | |
| { | |
| "territoryId": "GN", | |
| "regex": "\\d{3}" | |
| }, | |
| { | |
| "territoryId": "GP", | |
| "regex": "9[78][01]\\d{2}" | |
| }, | |
| { | |
| "territoryId": "GS", | |
| "regex": "SIQQ 1ZZ" | |
| }, | |
| { | |
| "territoryId": "GU", | |
| "regex": "969[123]\\d(?:[ \\-]\\d{4})?" | |
| }, | |
| { | |
| "territoryId": "GW", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "HM", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "IQ", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "KG", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "LR", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "LS", | |
| "regex": "\\d{3}" | |
| }, | |
| { | |
| "territoryId": "MG", | |
| "regex": "\\d{3}" | |
| }, | |
| { | |
| "territoryId": "MH", | |
| "regex": "969[67]\\d(?:[ \\-]\\d{4})?" | |
| }, | |
| { | |
| "territoryId": "MN", | |
| "regex": "\\d{6}" | |
| }, | |
| { | |
| "territoryId": "MP", | |
| "regex": "9695[012](?:[ \\-]\\d{4})?" | |
| }, | |
| { | |
| "territoryId": "MQ", | |
| "regex": "9[78]2\\d{2}" | |
| }, | |
| { | |
| "territoryId": "NC", | |
| "regex": "988\\d{2}" | |
| }, | |
| { | |
| "territoryId": "NE", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "VI", | |
| "regex": "008(?:(?:[0-4]\\d)|(?:5[01]))(?:[ \\-]\\d{4})?" | |
| }, | |
| { | |
| "territoryId": "PF", | |
| "regex": "987\\d{2}" | |
| }, | |
| { | |
| "territoryId": "PG", | |
| "regex": "\\d{3}" | |
| }, | |
| { | |
| "territoryId": "PM", | |
| "regex": "9[78]5\\d{2}" | |
| }, | |
| { | |
| "territoryId": "PN", | |
| "regex": "PCRN 1ZZ" | |
| }, | |
| { | |
| "territoryId": "PW", | |
| "regex": "96940" | |
| }, | |
| { | |
| "territoryId": "RE", | |
| "regex": "9[78]4\\d{2}" | |
| }, | |
| { | |
| "territoryId": "SH", | |
| "regex": "(?:ASCN|STHL) 1ZZ" | |
| }, | |
| { | |
| "territoryId": "SJ", | |
| "regex": "\\d{4}" | |
| }, | |
| { | |
| "territoryId": "SO", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "SZ", | |
| "regex": "[HLMS]\\d{3}" | |
| }, | |
| { | |
| "territoryId": "TC", | |
| "regex": "TKCA 1ZZ" | |
| }, | |
| { | |
| "territoryId": "WF", | |
| "regex": "986\\d{2}" | |
| }, | |
| { | |
| "territoryId": "XK", | |
| "regex": "\\d{5}" | |
| }, | |
| { | |
| "territoryId": "YT", | |
| "regex": "976\\d{2}" | |
| } | |
| ]; | |
| PostalCode.match = function(postalCode : String, territoryId : String) : Object { | |
| if (!postalCode || !_.isString(postalCode)) { | |
| return false; | |
| } | |
| let patterns = []; | |
| if (territoryId && _.isString(territoryId)) { | |
| if (territoryId.length==2) { | |
| let pattern : Object = _.find(PostalCode.patterns, function(v){if (territoryId == v.territoryId){return true;} return false;}); | |
| if (!pattern) { | |
| return false; | |
| } | |
| patterns.push(pattern); | |
| } else { | |
| return false; | |
| } | |
| } else { | |
| patterns = PostalCode.patterns; | |
| } | |
| //Logger.debug("patterns: {0}", JSON.stringify(patterns)); | |
| return _.find(patterns, function(v){ | |
| let regex : RegExp = new RegExp("^"+v.regex+"$", "i"), | |
| matches = postalCode.match(regex); | |
| //Logger.debug("territoryId: {0} regex: {1} matches: {2}", v.territoryId, regex, JSON.stringify(matches)); | |
| return (matches && matches.length>0 && matches[0]==postalCode); | |
| }); | |
| }; | |
| PostalCode.validate = function(postalCode : String, territoryId : String) : Boolean { | |
| return !!(PostalCode.match(postalCode, territoryId)); | |
| }; | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment