6 Replies Latest reply on Sep 10, 2014 2:36 PM by chcsep

    Formatting a text as phone number

    BatuhanGizer

      Title

      Formatting a text as phone number

      Post

      Hi,

      I am trying to change the format of a field.  The field is used for entering telephone numbers for clients but I was wondering if there is any possibility to format the field so after entering the 10 digit number, the field will automatically look like this :

      (000) 555-9999

      Thanks!

       

        • 1. Re: Formatting a text as phone number
          philmodjunk

          Set up this auto-enter calculation:

          Let ( [ e = Position ( Self ; "e" ; 1 ; 1 ) ;
                    P1a = If ( e ; Left ( Self; e -1 ) ; Self ) ;
                    P1 = Filter ( P1a ; 1234567890 ) ;
                    P2 = If ( e ; Right ( Self ; Length ( Self ) - Length ( P1a ) ) ) ] ;
                   Case ( Length ( P1 ) = 10 ; "(" & Left ( P1 ; 3 ) & ") " & Middle ( P1 ; 4 ; 3 ) & "-" & Right ( P1 ; 4 ) ;
                              Length ( P1 ) = 7 ;"(209) " & Left ( P1 ; 3 ) & "-" & Right ( P1 ; 4 ) ;
                              P1
                            ) // case
                   & If ( not IsEmpty ( P2 ) ; " " & P2 ; "" )
                  ) // let

          In addition to what you've described, it ignores formatting any text following the first instance of the letter "e" such as 555-4444 ext 345. Since this is for the 209 area code, it also adds the 209 area code for all numbers where these three letters were omitted.

          • 2. Re: Formatting a text as phone number
            davidanders

            Often developers use two fields for phone number entry. (depending if US or international)

            PhoneArea  and PhoneNumber are most often used for labels

            A Calculation field will create the field Phone from "(" & PhoneArea & ") " & PhoneNumber

            If the user can not be trusted to enter the dash in PhoneNumber, there are further options.

            • 3. Re: Formatting a text as phone number
              BatuhanGizer

              Works great and thanks for the quick reply.  I just want to ask, for learning purposes, can you explain what that code does for me if it wont take much of your time (what are P1's, e's etc)? I just want to learn as I go along and learn FileMaker.

              Thanks again!

              • 4. Re: Formatting a text as phone number
                philmodjunk

                I'm using the let function to assign values to variables to simplify the final expression and to avoid performing the same function call over and over again.

                e is the position of the first letter e in the field.

                p1a is all the phone number characters to the left of this "e".

                p1 is P1a with all non numeric digits "filtered" out.

                p2 is any "extension" that was entered, including that letter "e".

                IF there are exactly 10 digits to p1, the necessary () and - is added. If there are exactly 7 digits added, a default area code is added. IF there is an extension in p2, it is tacked on the end of all this with a space separating it from the rest of the phone number.

                • 6. Re: Formatting a text as phone number
                  chcsep

                  Sorry to dig up an old thread, but this is the one I found first that worked for my purposes and I have a variant.

                  If you replace the "e" (including the quotation marks) with "Left ( Filter ( Self ; "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ) ; 1 )  ; 1 ; 1 )" (excluding the outside quotation marks), it will preserve any data after* the phone number, for example if there is a mix of "ext", "x" and "p" (or other notes).

                  Here's the full thing, using the 123-456-7890 format, rather than the (123) 456-7890 format.

                  Let ( [ e = Position ( Self ; Left ( Filter ( Self ; "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ) ; 1 )  ; 1 ; 1 ) ;
                            P1a = If ( e ; Left ( Self; e -1 ) ; Self ) ;
                            P1 = Filter ( P1a ; 1234567890 ) ;
                            P2 = If ( e ; Right ( Self ; Length ( Self ) - Length ( P1a ) ) ) ] ;
                           Case ( Length ( P1 ) = 10 ; Left ( P1 ; 3 ) & "-" & Middle ( P1 ; 4 ; 3 ) & "-" & Right ( P1 ; 4 ) ;
                                      Length ( P1 ) = 7 ;"(209) " & Left ( P1 ; 3 ) & "-" & Right ( P1 ; 4 ) ;
                                      P1
                                    ) // case
                           & If ( not IsEmpty ( P2 ) ; " " & P2 ; "" )
                          ) // let
                   

                   

                  *providing there are no letters in the phone numbers (202a555b1234 won't work) and anything after the 10-digit number starts with a letter (202-555-1234 -ext 333 would remove the dash before the ext)