/ Published in: ActionScript 3
URL: http://regexlib.com/REDetails.aspx?regexp_id=260
Expand |
Embed | Plain Text
var inputPostcode:String = "dn3 6gb"; trace("inputPostcode: "+inputPostcode); var checkedPostcode:String = validateUkPostcode(inputPostcode).toUpperCase(); if (checkedPostcode == "") { trace("No valid postcode was found"); } else { trace("checkedPostcode: "+checkedPostcode); } function validateUkPostcode(str:String):String { var returnString:String; var pattern:RegExp = /[a-zA-Z]{1,2}[1-9]{1}[0-9]?([ ]{1}[0-9]{1}[a-zA-Z]{2})/g; var result:Object = pattern.exec(str); if(result == null) { returnString = ""; } else { returnString = result[0]; } return returnString; }
Comments
Subscribe to comments
You need to login to post a comment.

NOTE: This is not a good regular expression to use to find matches of UK postcodes. For example, "EC1R 5DW" is an actual postcode, but this RegEx pattern does not find it! If anyone has a better RegEx pattern, please post it here.
You can test your regular expression patterns here ...
http://gskinner.com/RegExr/
and
http://design215.com/toolbox/regexp.php
The Wikipedia entry for 'Postcodes in the United Kingdom' (http://en.wikipedia.org/wiki/PostcodesintheUnitedKingdom) has the following Regular Expression, which seems pretty good ...
/[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}/i
I have updated the snippet the use this new RegEx.
http://en.wikipedia.org/wiki/UK_postcode