UK Postcode Validation in AS3

Let me file this post under boring but useful. Recently I worked on a project where I had to validate UK postcodes in flash. The method I used in the end was a regular expression I found, with a bit of formatting logic added. This should work for all UK postcodes.

var validPostcode:String   =  "LS12ED";
var validPostcode2:String   = "LS1 2ED";
var invalidPostcode:String =  "NOTAPOSTCODE";


function validateUkPostcode(str:String):Boolean {
	if(str.indexOf(" ")==-1){
		trace("Adding postcode space");
		var l:int = str.length;
		str=str.substr(0,l-3)+" "+str.substr(l-3,l);

	var pattern:RegExp =  /[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}/i

	var result:Object = pattern.exec(str);
	if(result == null) {
		return false;
	} else {
		return true;