Phone Number Formatting Sample Code

Mindwatering Incorporated

Author: Tripp W Black

Created: 10/04/2012 at 02:10 PM

 

Category:
Notes Developer Tips
Formulas

Issue:
Want to force phone numbers to a specific format:
919-123-4567 or 919.123.4567 to (919) 123-4567, and if has an extension, force to just "x" plus the number, e.g. x102, or 919-123-4567 x102.

Solution:
tmp:= @Trim(@ThisValue);
tmpjustnumbers:= @ReplaceSubstring(tmp; "(" : ")" : " ": "-" : "/" : "." : "_"; "");

phonenums:= @If(@Contains(tmpjustnumbers; "ext"); @Trim(@Left(tmpjustnumbers; "ext"));
@Contains(tmpjustnumbers; "x"); @Trim(@Left(tmpjustnumbers; "x"));
tmpjustnumbers);

phonenumsext1:= @If(@Contains(tmp; "ext"); @Trim(@Right(tmp; "ext"));
@Contains(tmp; "x"); @Trim(@Right(tmp; "x"));
"");
phonenumsext:= @If(phonenumsext1 = ""; ""; "x" + phonenumsext1);

len := @Length(phonenums);
newphonenbr := @If(len = 7 ; @Left(phonenums ; 3) + "-" + @Right(phonenums ; 4);
len = 10; "(" + @Left(phonenums ; 3) + ") " + @Middle(phonenums ; 3 ; 3 ) + "-" + @Right(phonenums ; 4) ;
"");

@If(newphonenbr =""; tmp; @Trim( newphonenbr + " " + phonenumsext))


Can also use @Matches:
@If (@Matches (@ThisValue; “{0-9}{0-9}{0-9}{-}{0-9}{0-9}{0-9}{-}{0-9}{0-9}{0-9}{0-9});
@Success;
@Failure (“Phone numbers must be in xxx-xxx-xxxx format.”)
);


previous page