ActionScript 3 AS3格式化英国邮政编码(例如AA99 9AA)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ActionScript 3 AS3格式化英国邮政编码(例如AA99 9AA)相关的知识,希望对你有一定的参考价值。

var inputPostcode:String = "   e   c   1   r    5    d   w   ";
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 withNoSpaces:String = str.split(" ").join("");
	var outwardCode:String = withNoSpaces.substring(0, withNoSpaces.length-3);
	var inwardCode:String = withNoSpaces.substring(withNoSpaces.length-3, withNoSpaces.length);
	var formatted:String = outwardCode + " " + inwardCode;
	str = formatted.replace(/^\s+|\s+$/g, "");
	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) {
		returnString = "";
	} else {
		returnString = result[0];
	}
	return returnString;
}

// OUTPUT
// inputPostcode:    e   c   1   r    5    d   w   
// checkedPostcode: EC1R 5DW

以上是关于ActionScript 3 AS3格式化英国邮政编码(例如AA99 9AA)的主要内容,如果未能解决你的问题,请参考以下文章

ActionScript 3 AS3格式化英国手机号码(XXXXX XXXXXX)

AS3格式英国邮政编码(如AA99 9AA)

ActionScript 3 AS3 RegExp:验证荷兰邮政编码

AS3使用正则表达式检查并返回有效的英国邮政编码

格式化英国邮政编码以进行存储

ActionScript 3 AS3显示格式化百分比