1 Reply Latest reply on Jan 27, 2007 12:57 PM by perezsg

    PhoneFormatter Class needs work

    DangerAhead
      PROBLEM: The PhoneFormatter class chokes on non-numeric characters.

      when you try and format this string "(415) 555-1212". It throws an error.
      right at the beginning of the "format" function, the class checks to see if the incoming string is a number with this code:

      if (!value || String(value).length == 0 || isNaN(Number(value)))
      {
      error = defaultInvalidValueError;
      return "";
      }

      What it should do is strip all the non-numberic characters from the input string THEN run that error check. Instead it just returns "";
      Also, the code in that proc is really long(old) when some pattern matching using RegEx could cut that code down to 3-4 lines. I think that Class should be re-written.

      so you have to strip all the non-numeric characters from the string before you send it to the format() function.

      use this:
      public function stripToNumbersOnly(inputStr:String):String
      {
      // rips the non-numeric characters from a string
      // in preparation for PhoneFormatter (which sucks)
      var regex:RegExp = /\D/g;
      var retStr:String = inputStr.replace(regex,"");
      trace ("stripping: " + inputStr+ " = " + retStr);
      return retStr;
      }

      SURE WOULD BE NICE TO BE ABLE TO POST CODE IN THIS FORUM.....
        • 1. Re: PhoneFormatter Class needs work
          perezsg
          Good Point! I ran into this issue today when formatting the data entered by the end user.

          <mx:TextInput x="24" y="228" id="usr_phone" width="278" fontSize="9" toolTip="Enter Your Phone Number" focusOut="formatPhoneNumber(usr_phone)"/>

          public function formatPhoneNumber(item:TextInput):void
          {
          var remString:RegExp = /\D/g;
          var phoneFormatter:PhoneFormatter = new PhoneFormatter;
          phoneFormatter.validPatternChars = "#-() ";
          phoneFormatter.areaCode = -1;
          phoneFormatter.areaCodeFormat = "(###)";
          phoneFormatter.formatString = "(###) ###-####";

          var fString:String = item.text.replace(remString, '');

          item.text = phoneFormatter.format(fString);
          }