validatorjs / validator.js

String validation
MIT License
23.06k stars 2.3k forks source link

Improvement in postcode validation #722

Open chikochavda opened 7 years ago

chikochavda commented 7 years ago

Hello Right now we have only 34 country for postcode validation, Can we add more country support for post code validation,

I found link which has postcode validation with different country

Original SO question https://stackoverflow.com/questions/578406/what-is-the-ultimate-postal-code-and-zip-regex

Link for postcode regex source http://unicode.org/cldr/trac/browser/tags/release-26-0-1/common/supplemental/postalCodeData.xml?rev=11071

I have tried few of them and it works as expected.

Thanks.

Possible json for regex postcode

{ "JE":"JEd[dA-Z]?[ ]?d[ABD-HJLN-UW-Z]{2}", "GG":"GYd[dA-Z]?[ ]?d[ABD-HJLN-UW-Z]{2}", "IM":"IMd[dA-Z]?[ ]?d[ABD-HJLN-UW-Z]{2}", "BR":"d{5}[-]?d{3}", "AX":"22d{3}", "KR":"d{3}[-]d{3}", "CN":"d{6}", "SG":"d{6}", "AD":"ADd{3}", "AR":"([A-HJ-NP-Z])?d{4}([A-Z]{3})?", "AM":"(37)?d{4}", "AZ":"d{4}", "BH":"((1[0-2]|[2-9])d{2})?", "BD":"d{4}", "BB":"(BBd{5})?", "BY":"d{6}", "BM":"[A-Z]{2}[ ]?[A-Z0-9]{2}", "BA":"d{5}", "IO":"BBND 1ZZ", //not sure "BN":"[A-Z]{2}[ ]?d{4}", "BG":"d{4}", "KH":"d{5}", "CV":"d{4}", "CL":"d{7}", "CR":"d{4:5}|d{3}-d{4}", "HR":"d{5}", "CY":"d{4}", "DO":"d{5}", "EC":"([A-Z]d{4}[A-Z]|(?:[A-Z]{2})?d{6})?", "EG":"d{5}", "EE":"d{5}", "FO":"d{3}", "GE":"d{4}", "GL":"39d{2}", "GT":"d{5}", "HT":"d{4}", "HN":"(?:d{5})?", "HU":"d{4}", "ID":"d{5}", "JO":"d{5}", "KZ":"d{6}", "KW":"d{5}", "LA":"d{5}", "LV":"d{4}", "LB":"(d{4}([ ]?d{4})?)?", "LT":"d{5}", "LU":"d{4}", "MK":"d{4}", "MY":"d{5}", "MV":"d{5}", "MT":"[A-Z]{3}[ ]?d{2:4}", "MU":"(d{3}[A-Z]{2}d{3})?", "MD":"d{4}", "MC":"980d{2}", "MA":"d{5}", "NP":"d{5}", "NZ":"d{4}", "NI":"((d{4}-)?d{3}-d{3}(-d{1})?)?", "NG":"(d{6})?", "OM":"(PC )?d{3}", "PK":"d{5}", "PY":"d{4}", "PH":"d{4}", "PR":"00[679]d{2}([ -]d{4})?", "SM":"4789d",//not sure "SN":"d{5}", "SK":"d{3}[ ]?d{2}", "SI":"d{4}", "LK":"d{5}", "TJ":"d{6}", "TH":"d{5}", "TN":"d{4}", "TR":"d{5}", "TM":"d{6}", "UA":"d{5}", "UY":"d{5}", "UZ":"d{6}", "VA":"00120",//not sure "VE":"d{4}", "AS":"96799",//not sure "CC":"6799",//not sure "CK":"d{4}", "RS":"d{6}", "ME":"8d{4}", "CS":"d{5}", "YU":"d{5}", "CX":"6798",//not sure "ET":"d{4}", "FK":"FIQQ 1ZZ",//not sure "NF":"2899",//not sure "FM":"(9694[1-4])([ -]d{4})?", "GF":"9[78]3d{2}", "GN":"d{3}", "GP":"9[78][01]d{2}", "GS":"SIQQ 1ZZ",//not sure "GU":"969[123]d([ -]d{4})?", "GW":"d{4}", "HM":"d{4}", "IQ":"d{5}", "KG":"d{6}", "LR":"d{4}", "LS":"d{3}", "MG":"d{3}", "MH":"969[67]d([ -]d{4})?", "MN":"d{6}", "MP":"9695[012]([ -]d{4})?", "MQ":"9[78]2d{2}", "NC":"988d{2}", "NE":"d{4}", "VI":"008(([0-4]d)|(5[01]))([ -]d{4})?", "PF":"987d{2}", "PG":"d{3}", "PM":"9[78]5d{2}", "PN":"PCRN 1ZZ", "PW":"96940", "RE":"9[78]4d{2}", "SH":"(ASCN|STHL) 1ZZ", "SJ":"d{4}", "SO":"d{5}", "SZ":"[HLMS]d{3}", "TC":"TKCA 1ZZ", "WF":"986d{2}", "XK":"d{5}", "YT":"976d{2}" }

linkinmedo commented 7 years ago

I'll look into it tomorrow.

chikochavda commented 7 years ago

Thanks,

You can additional missing post code from

https://unipaygateway.info/countries

https://en.wikipedia.org/wiki/List_of_postal_codes

I am working on complete list for my own work, If i finish it then i will post it here , Thanks. ref link for country list : Here

countryWithNoPostcode = [ "ZW", "YE", "EH", "VU", "UM", "AE", "UG", "TV", "SS", "SY", "TZ", "TO", "TK", "TG", "TL", "SL", "SB", "SX", "SC", "ST", "KN", "RW", "QA", "AO", "AG", "AW", "BS", "BZ", "BJ", "BO", "BQ", "BW", "CM", "BI", "BF", "CF", "TD", "CG", "CD", "CI", "KM", "CW", "DJ", "DM", "GQ", "ER", "FJ", "GA", "GM", "GH", "TF", "GD", "GY", "HK", "IE", "KI", "KP", "LY", "MO", "MW", "ML", "MR", "NR", "BV", "NU", "SR" ];

This all are renaming post codes regex,

    missingCodes = {

    GI: "GX11 1AA", //single code used for entire       
    AI: "AI-2640", //single code used for entire 
    AQ: "BIQQ 1ZZ",//single code used for entire 
    BL: "^97133$",//single code used for entire 
    MF: "^97150$",//single code used for entire     
    JM: "^[0-9]{2}$",
    PS: "^[0-9]{3}$",
    AF: "^[0-9]{4}$",
    AL: "^[0-9]{4}$",
    AT: "^[0-9]{4}$",
    SN: "^[0-9]{5}$",
    BT: "^[0-9]{5}$",
    PE: "^[0-9]{5}$",
    MM: "^[0-9]{5}$",
    CU: "^[0-9]{5}$",       
    VN: "^[0-9]{6}$",
    MZ: "^[0-9]{6}$",
    CO: "^[0-9]{6}$",
    PA: "^[0-9]{6}$",       
    IR: "^[0-9]{10}$",

    TT: "^[0-9]{6}$",
    NA: "^9[0-2][0-9]{2}$",
    SV: "^01101$",// NNNN       
    MS: "^[Mm][Ss][Rr] 1110-1350$",
    KY: "^[Kk][Yy][0-9]-[0-9]{4}$",
    VC: "^[Vv][Cc][0-9]{4}$",
    VG: "^[Vv][Gg]11([1-5][0-9]|60)$", 
    WS: "^[A-Z][A-Z][0-9]{4}$", 
    LC : "^[Ll][Cc][0-9]{2} [0-9]{4}" 
     }
linkinmedo commented 7 years ago

dude this will take too much time specially because I can't just take the list above and use it since when I tried the GB regex from the links above the tests failed which means that I'll have to go over them one by one. sorry but I can't spend that much time on it.

chikochavda commented 7 years ago

I completed postcode validation for my work , May be i can pull and add related post code in list. I will check each regex and create test case (I have to do it anyways since i have to implement it). Then post a link for Testcase. Thanks.

linkinmedo commented 7 years ago

@chikochavda that would be great, thank you in advance.

chikochavda commented 7 years ago

Hello, You were right some of the test cases were failing in the list,

This is working list for all pincodes regex with test cases,

https://jsfiddle.net/ys7mtjsr/

I skipped few of the countries which belongs to your implementation (34 in total). I skipped few of those country which support only digits as pincodes as it not that complex (4 digit, 5 digits, 6 digits)(98 countries)

Thanks

linkinmedo commented 7 years ago

@chikochavda man you don't use ^ and & which result in alot of false positives, you also don't surround your or statement in brackets which also results in false positives. and in the case of (BH, AR, BB, EC, HN, LB, MU, NI, NG, FM) you surround the whole thing then use ? which makes it always true since there is no way to fail.

That's what it found just by quickly running some tests, so I can't just use your list. since most of the list is just digits I'll take a look at them later and run over the non digits ones to make sure they are good.

Thank you for the list, I just wanted to let you know about these issues incase you are using your list in something important.

dantman commented 6 years ago

CLDR's postcode list is not maintained and was removed.

I actually ditched validator's post code validation and implemented validation using data from Google's libaddressinput. Which provided me with validation for zip/postal/eircode/pin codes from countless countries.