FixPhoneNumberFormatUS ( Number )
Customizable function to standardize raw phone number input to 123-456-7890 x123
Be the first to rate this function Log in to vote
|
RealGrouchy - Show more from this author
https://realgrouchy.blogspot.com |
Function definition: (Copy & paste into FileMaker's Edit Custom Function window)
Garbage in, phone number out. This custom function I built to standardize phone number inputs has served me well for over 10 years.
This custom function is used to standardize the formatting of a 7- or 10-digit Canada/US* phone number input by changing the format to "321-456-7890 x123". (*non-Canada/US phone numbers are left intact.)
The extension separator can be customized (x by default), and if most of your contacts are local you can add a default local area code (disabled by default).
The following examples are included in the script comments:
EXAMPLE INPUTS/OUTPUTS:
INVALID RESULTS DUE TO LETTERS IN OR BEFORE PHONE NUMBER:
The one situation where data loss may occur is if someone uses letters instead of numbers for their number, deliberately or by accident. See the section on International phone numbers for more info on how non-7 or 10 digit numbers are handled.
1 (800) 555-SMRT —> 1-800-555 (Interpreted as international number, letters stripped)
800-555-SMRT (7678) —> +800-555 x7678 (interprets as 6-digit international number, numbers after letter treated as extension)
321h456-7890 —> 321 x4567890 (interprets as 6-digit international number, numbers after letter treated as extension)
(321) 456-o789 —> 321-456 x789 (interprets as 6-digit international number, numbers after letter treated as extension)
TEN-DIGIT PHONE NUMBERS:
Any string with 10 numbers and no letters will return in format 321-456-7890, including:
Any similar string with 11 numbers that starts with a 1 or +1 will have that part stripped off
3124567890
(321) 456-7890
321.456.7890
321 456 7890
(321)- 456-7890
321)456-7890
321-456-78-90
1 (321) 456-7890
1-321-4567890
+1-(321)-456.7890
13214567890
—————————
= 321-456-7890
SEVEN-DIGIT PHONE NUMBERS:
Any string with 7 numbers and no letters will add an area code and convert to format 312-456-7890, including:
This can be disabled by removing or commenting out the line that starts with "Length ( P1 ) = 7 ; …"
4567890
456-7890
(etc.)
—————————
= 321-456-7890
MISC:
Any text will be stripped if there are no numbers after it:
321-456-7890 (cell)
456-7890 - sister
—————————
= 321-456-7890
All-zero numbers will return an empty result:
000-000-0000
—————————
= ""
PHONE EXTENSIONS:
Any numbers that come after any text (where 'text' includes letters and the characters in the xFiltercharacters variable above, i.e. /, \, #, comma, or *) will be interpreted as an extension in the format 321-456-7890 x123 and any non-numbers will be stripped (extension separator can be customized by changing the xFormat variable above):
321-456-7890x123 (home)
321-456-7890*123
321-456-7890 # 123
321-456-7890 ,,,*xabcdef,, 123
321-456-7890 p.123
1321-456-7890 ext. 123
+1-321-456-7890 local 123
456-7890 ext. 123
456780/123
—————————
= 321-456-7890 x123
The exception to this is if there are no numbers before the first character. If that's the case ALL numbers after the first character are treated as the phone number (no extension)
(cell) (321) 456-7890 —> 321-456-7890
work: 321-456-7890 ext. 123 —> 321-456-7890-123 (treats as international)
NOTE: the following scenarios may not work as intended:
(321) 456-7890 (cell) —> 321-456-7890
(cell) (321)456-7890
321-456-7890 / 800-456-7890 —> 321-456-7890 x8004567890
321-456-7890 press 1 x 234 —> 321-456-7890 x1234
(321) 456-7890 1234 —> 321-456-7890-1234 (interpreted as international number)
321h456-7890 —> 321 x4567890 (interprets 3 numbers before letter as international number)
1234 (office) —> "1234" (interpreted as international number with characters before first letter unchanged)
NON-CANADA/US ("INTERNATIONAL") PHONE NUMBERS:
A number will be treated as International if it starts with + (except +1) or doesn't have 7 or 10 characters (or 11 starting with 1).
This will substitute any non-numbers after the + and before the extension into hyphens, and will strip out any non-numbers from the extension:
011-01-234-567-890 —> Unchanged
+44 012345.67890 —> +44-012345-67890
+(44) 0123-4567-890- —> +44-0123-4567-890
12(345) 6789 0123 —> 12-345-6789-0123
(21)-34 —> 21-34
21.34 poste 3(11) —> 21-34 x311
1234 (cell) —> 1234
NOTE: If an international phone number has 7 or 10 digits but does not start with +, it will be interpreted as North American!
64 9 123 4567 —> 649-123-4567
+1-321-456-7890 —> 321-456-7890
NOTE: If there are a lot of non-number characters, the calculation will only appear any that are found in the first 20 non-number characters:
123()()()()()()()()()456 —> 123-456 (because both "(" and ")" appear in the first 20 non-number characters
123()()()()()()()()()()()()()()..456 —> 123-..456 (because "." does not appear in the first 20 non-number characters. If your input has this many non-number characters you have a very strange phone number input!)
Comments
Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.