Well... > I've just found the PAF digest info which defines the > structure of a postcode, so I'll post my version of the regex > shortly... :o)
I was bored over lunch, so came up with this little UDF to validate postcodes. It checks that the postcode is generally well formatted according to the information given on page 6 of the following Royal Mail PAF document (PDF): http://short.badpen.com/?YMHQAJUU (which was correct as of Dec. 2000 and I can find no update). It also checks that the postcode area code (the first 1/2 alpha characters of the left-hand bit of the postcode or outcode) are valid according to the list of 124 area codes. It might seem fairly long-winded compared to a small little regex, it doesn't let things like the GIRO and Santa codes through, and I'm sure there are better ways to do it, but (AFAIK) it works and it's better than nothing! One thing that could easily be done is to squish all the outcode regex's in to one rather than "generating" then each time... From there you could then stick it all in the one regex to end up with something huge and unwieldy but better than lots of loops each time... Any comments grudgingly received... ;o) Tim. <cfscript> function validatepostcode(pc) { // the list of valid postcode areas (124 of them...) pcalist = "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,GU,GY,HA,HD,HG,HP,HR,HS,HU ,HX,IG,IM,IP,IV,JE,KA,KT,KW,KY,L,LA,LA,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,Y O,ZE"; // outcode formats pcout = "AN,ANN,AAN,AANN,ANA,AANA"; // make sure the basic syntax is OK including the incode characters (no C,I,K,M,O,V) if (NOT refind('^([[:digit:]A-Z]){2,4}[ ]{1}[[:digit:]][ABDEFGHJLNPQRSTUWXYZ]{2}$', pc)) { return false; } // loop through each outcode format for (i = 1; i LTE listlen(pcout); i = i+1) { // replace the A/N placeholders with valid class names regex = replacelist(listgetat(pcout, i), 'A,N', '[A-Z],[[:digit:]]'); // check if it matches if (refind(regex, listgetat(pc, 1, ' '))) { // now grab the given postcode area pca = refind('^([A-Z]+)', pc, 1, true); if (arraylen(pca.len)) { // check that the postcode area is valid if (listfind(pcalist, left(pc, pca.len[1]))) { // yes, so we've got a valid postcode! return true; } } } } // if we get here then fail return false; } </cfscript> ------------------------------------------------------- OUR NEW SITE IS NOW LIVE Visit our new website at http://www.rawnet.com/ and race around the beautiful Bracknell streets at http://xmas.rawnet.com/ ------------------------------------------------------- Tim Blair Web Application Engineer, Rawnet Limited Direct Phone : +44 (0) 1344 393 441 Switchboard : +44 (0) 1344 393 040 ------------------------------------------------------- This message may contain information which is legally privileged and/or confidential. If you are not the intended recipient, you are hereby notified that any unauthorised disclosure, copying, distribution or use of this information is strictly prohibited. Such notification notwithstanding, any comments, opinions, information or conclusions expressed in this message are those of the originator, not of rawnet limited, unless otherwise explicitly and independently indicated by an authorised representative of rawnet limited. ------------------------------------------------------- -- ** Archive: http://www.mail-archive.com/dev%40lists.cfdeveloper.co.uk/ To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] For human help, e-mail: [EMAIL PROTECTED]
