formatPhone ( )

Function stats

Average user rating
626
432
9999
Support
FileMaker 10.0 +
Date posted
26 May 2020
Last updated
26 May 2020
Version
Recursive function
No

Author Info
 Nolee

3 functions

Average Rating None

author_avatar



 

Function overview

Prototype

formatPhone  ( defaultAreaCode;   phoneNumber )


Parameters

defaultAreaCode  The default area code to apply if the provided phone number has seven digits


phoneNumber  The phone number to format


Description

Tags:  phone  

Formats a phone number to the common US format [ e.g., (555) 555-5555]. It will strip out any non-numeric characters before applying the new format. If the number of resulting digits = 10, this function will use the first three digits as the area code, followed by the seven digit phone number. Optionally, a default area code [defaultAreaCode] can be provided (most likely from a global field), and will be prepended to seven digit phone numbers. Phone numbers that are not seven or 10 digits long will result in an error, which will show as the phone number with question marks at the beginning and end [e.g., ?55555555?]. A phone number text field can be automatically formatted with this function as a calculated value if the "Do not replace existing value (if any)" option is uncheck.

Examples

Sample input

*555-555=5555


Sample output

(555) 555-5555

 

Function code

// Formats a phone number to the common US format [ e.g., (555) 555-5555]. It will strip out any non-numeric characters before applying the new format. If the number of resulting digits = 10, this function will use the first three digits as the area code, followed by the seven digit phone number. Optionally, a default area code [defaultAreaCode] can be provided (most likely from a global field), and will be prepended to seven digit phone numbers. Phone numbers that are not seven or 10 digits long will result in an error, which will show as the phone number with question marks at the beginning and end [e.g., ?55555555?]. A phone number text field can be automatically formatted with this function as a calculated value if the "Do not replace existing value (if any)" option is uncheck.

Let ( [

    ~pn = Filter ( phoneNumber ; "0123456789" ) ; //strip out extraneous characters
    ~ac = If ( IsEmpty ( defaultAreaCode ) ; "" ; "(" & defaultAreaCode & ") " ) ; //set the default area code
    ~lenPn = Length ( ~pn ) //get the length of the unformatted phone number
] ;

    If ( not IsEmpty ( ~pn ) ; //if the phone number isn't empty, apply the format
        Case
            (
            //if the length of the phone number = 10, format using the first three digits as the area
            //code, and the last seven digits as the phone number
            ~lenPn = 10 ; "(" & Left ( ~pn ; 3 ) & ") " & Middle ( ~pn ; 4 ; 3 ) & "-" & Right ( ~pn ; 4 ) ;

            //if the length of the phone number = 7, format using the default area code (if provided)
            ~lenPn = 7 ; ~ac & Left ( ~pn ; 3 ) & "-" & Right ( ~pn ; 4 ) ;

            //if the length of the phone number isn't 7 or 10, produce an error
            "?" & ~pn & "?"
        ) ;
    ""
    )
)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/432

    Prototype: formatPhone( defaultAreaCode; phoneNumber )
    Function Author: Nolee (http://www.fmfunctions.com/mid/626)
    Last updated: 26 May 2020
    Version: 1.4

*/
// ===================================

 

 

 

 

 

 

 

Top Tags

Text Parsing  (33)
List  (32)
Date  (30)
XML  (27)
Format  (23)
Sql  (22)
Dev  (20)
Debug  (17)
Layout  (15)
Interface  (15)
Text  (14)
Variables  (13)
Filter  (12)
Design  (11)
Layout Objects  (11)
Array  (8)