Jump to content

Format Phones and Suppress Label


AngelBenitez

Recommended Posts

Hello,

I am creating a business card with the option to have direct, mobile, and fax numbers. The user can choose to have all three, two, or only one.

The card, however, needs to have a label before the actual number (ex. Direct 000.000.0000 Mobile 111.111.1111 Fax 222.222.2222)

I am trying to create a rule that does the following:

Give the user to choose the phone numbers they would like to have; however, the numbers that are pulled from Excel aren't formatted as I would like them to appear (They currently are formatted as ( (000) 000-0000), but I want them to be formatted to include dots in between. Also, if they don't want to have a certain number, I want it to suppress the label.

Example:

John wants to only to have his mobile and fax number; therefore, it would look like:

Mobile 111.111.1111 Fax 222.222.2222

However, if he wanted direct, mobile, and fax, it would look like this:

Direct 000.000.0000 Mobile 111.111.1111 Fax 222.222.2222

Link to comment
Share on other sites

  • 2 weeks later...

These sorts of formats are often pretty specific to each piece. Here's a rule that would work for this one but would need a lot of adjusting if the format changed much.

function format_numbers(label_number)
{
    var output = "";

    for (i = 1; i < label_number.length; i += 2)
    {
        var n = label_number[i].replace(/\D/g, ""); //remove any formatting from number
        
        if (n.length > 6) //check to see if number is present
        {
            output += '<b>' + label_number[i -1] + '</b>' + " "; //set bold label + space
            
            if (n[0] == "1") //check if number starts with a 1
                output += "1.";
                
            if (n.length > 7) //check for area code
                output += n.substr(-10,3) + ".";
            
            output += n.substr(-7,3) + "." +n.substr(-4,4); //split number up and add periods
            
            if (i < label_number.length -1) //add a seperator space between numbers if its not the last one
                output += " ";
        }
    }
    
    return output;
}

//format as [Label, Number, Label, Number, etc];
return format_numbers(["Direct", Field("Direct Number"), "Mobile", Field("Mobile Number"), "Fax", Field("Fax Number")]);

 

Link to comment
Share on other sites

Thomas's solution is good, but there's a handy FormatPhoneNumber function built into FusionPro which will do most of the heavy lifting for you.  So you should be able to just do something like this:

var nums = {
  "Direct": Field("Direct Number"),
  "Mobile": Field("Mobile Number"),
  "Fax": Field("Fax Number"),
};

var result = [];
for (var label in nums)
{
    if (nums[label])
        result.push(label.bold() + " " + FormatPhoneNumber(nums[label]));
}
return result.join(" ");
 
Link to comment
Share on other sites

My biggest issue with the FormatPhoneNumber function is when you have a 1 it forces the format to start with +1 . I suppose you could use a replace on it, in this case:

result.push(label.bold() + " " + FormatPhoneNumber(nums[label]).replace("+1 ", "1."));

It is rather annoying though, I wish there was a FormatPhone function that worked more like the FormatNumber one where you can just use something like FormatPhone("1.###.###.####", num).

Also, the .bold() is new to me, apparently its an old deprecated function in javascript, but seems super useful in FusionPro. Thanks for that!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...