AS3 Checks and Returns Valid UK Postcode using Regular Expression


/ Published in: ActionScript 3
Save to your folder(s)



Copy this code and paste it in your HTML
  1. var inputPostcode:String = "dn3 6gb";
  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.  
  12.  
  13. function validateUkPostcode(str:String):String {
  14. var returnString:String;
  15. var pattern:RegExp = /[a-zA-Z]{1,2}[1-9]{1}[0-9]?([ ]{1}[0-9]{1}[a-zA-Z]{2})/g;
  16. var result:Object = pattern.exec(str);
  17. if(result == null) {
  18. returnString = "";
  19. } else {
  20. returnString = result[0];
  21. }
  22. return returnString;
  23. }

URL: http://regexlib.com/REDetails.aspx?regexp_id=260

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.