> Problem is that it also permits post codes like "W3NEREF8
> 6TXSDF", because within the string it can find at least one
> sequence specified in the RegEx.
Stick ^ at the beginning and $ at the end:
^[A-Z]{1,2}\d[A-Z\d]? \d[ABD-HJLNP-UW-Z]{2}$
^ = start of string
$ = end of string
Or you could try something like this that does a more "complete" job by
checking not only that it's valid with regard to letters/numbers, but
also that it doesn't contain any invalid characters in the wrong place
and that the out- and in-code parts are valid. (watch the wrap!)
<!--- Validate a UK postcode --->
<cffunction name="postcodeValidate" access="public" returntype="boolean"
output="no" hint="Validate a UK postcode">
<cfargument name="postcode" type="string" required="yes" hint="The
postcode to validate">
<cfscript>
// localise vars
var i = "";
var pca = "";
var regex = "";
// the list of valid postcode areas (124 of them...)
var 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
var 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}$', arguments.postcode)) {
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(arguments.postcode, 1, ' '))) {
// now grab the given postcode area
pca = refind('^([A-Z]+)', arguments.postcode, 1, true);
if (arraylen(pca.len)) {
// check that the postcode area is valid
if (listfind(pcalist, left(arguments.postcode,
pca.len[1]))) {
// yes, so we've got a valid postcode!
return true;
}
}
}
}
// if we get here then fail
return false;
</cfscript>
</cffunction>
Tim.
-------------------------------------------------------
Try the ColdFusion Coding Contest! Current contest:
Maze Solver - http://tech.badpen.com/cfcontest/
-------------------------------------------------------
RAWNET LTD - Internet, New Media and ebusiness Gurus.
WE'VE MOVED - for our new address, please visit our
website at http://www.rawnet.com/ or call us any time
on 0800 294 24 24.
-------------------------------------------------------
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]