AS3 Format a UK Mobile Number (XXXXX XXXXXX)


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

This returns a string with a single space after the fifth character.
Useful for displaying a UK mobile number which is easy to read.


Copy this code and paste it in your HTML
  1. function formattedMobileNum($numStr:String):String {
  2. var withNoSpaces:String = $numStr.split(" ").join("");
  3. var part1:String = withNoSpaces.substr(0, 5);
  4. var part2:String = withNoSpaces.substr(5, withNoSpaces.length-1);
  5. var formatted = part1 + " " + part2;
  6. formatted = formatted.replace(/^\s+|\s+$/g, "");
  7. return formatted;
  8. }
  9.  
  10. var mobileNum:String = " 0 785 41 23 456 ";
  11. trace(formattedMobileNum(mobileNum));
  12. // OUTPUT
  13. // 07854 123456

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.