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

Posted

tags:

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

This code removes all the spaces and then inserts a single space so that the there are only 3 characters on the right-hand side (the inward code).
  1. var inputPostcode:String = " e c 1 r 5 d w ";
  2. trace("inputPostcode: "+inputPostcode);
  3. var checkedPostcode:String = validateUkPostcode(inputPostcode).toUpperCase();
  4.  
  5. if (checkedPostcode == "") {
  6. trace("No valid postcode was found");
  7. } else {
  8. trace("checkedPostcode: "+checkedPostcode);
  9. }
  10.  
  11. function validateUkPostcode(str:String):String {
  12. var returnString:String;
  13. var withNoSpaces:String = str.split(" ").join("");
  14. var outwardCode:String = withNoSpaces.substring(0, withNoSpaces.length-3);
  15. var inwardCode:String = withNoSpaces.substring(withNoSpaces.length-3, withNoSpaces.length);
  16. var formatted:String = outwardCode + " " + inwardCode;
  17. str = formatted.replace(/^s+|s+$/g, "");
  18. var pattern:RegExp = /[A-Z]{1,2}[0-9R][0-9A-Z]? [0-9][ABD-HJLNP-UW-Z]{2}/i;
  19. var result:Object = pattern.exec(str);
  20. if(result == null) {
  21. returnString = "";
  22. } else {
  23. returnString = result[0];
  24. }
  25. return returnString;
  26. }
  27.  
  28. // OUTPUT
  29. // inputPostcode: e c 1 r 5 d w
  30. // checkedPostcode: EC1R 5DW

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

使用正则表达式识别英国邮政编码中的模式

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

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

AS3格式化英国移动电话号码(XXXXXXXXXX)

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

从英国邮政编码获取进出代码