Jump to content

Formatting phone number fields


GSBjay

Recommended Posts

Posted

Hey guys,

I'm trying to find a script to format some phone numbers in a business card but I'm new to javascripts. In the example below, there can be 3 instances.

 

Instance 1: (if all 3 numbers exist then it would look like this, with the fax breaking to a new line)

 

<phone> | <mobile>

<fax>

 

 

Instance 2: (if only phone and fax then it would look like this)

 

<phone> | <fax>

 

 

Instance 3: (if only phone and mobile then it would look like this)

 

<phone> | <mobile>

 

 

I've been given a code by the support group but it's not behaving well.

 

if(Field("Mobile") !="")

return 'T '+Field("telephone") + ' | M '+Field("Mobile") + '<br>F ' + Field("Fax");

else

return 'T '+Field("telephone") + ' | F '+Field("Fax");

 

The double blank space I put in ' | M ' is somehow purged when previewed.

Does anyone know how to retain the blank spaces?

 

Thanks,

Jay

Posted

The double blank space I put in ' | M ' is somehow purged when previewed.

Does anyone know how to retain the blank spaces?

To insert more than one space, you need to use the HTML entity for a non-breaking space (in place of an actual space) and enable tagging by checking the appropriate box in the rule window:

 

Posted

Thanks Eric,

I've updated the script with your instructions and it retained the blank spaces. After testing the card, I realize instance 3 is not working. When only <telephone> and <mobile> is present, it looks like this:

 

<telephone> | <mobile>

F

The letter "F" appears in its own line when no fax number is present.

 

 

This is my current script.

if(Field("Mobile") !="")

return 'T '+Field("telephone") + "  |  M "+Field("Mobile") + '<br>F ' + Field("Fax");

else

return 'T '+Field("telephone") + "  |  F "+Field("Fax");

I do not have a script for instance 3. Do anyone know to make all 3 instances work?

 

Thanks,

Jay

Posted

Jay give this a shot.

 

Step 1:

Create a RULE just for the Fax:

 

if(Field("Fax") !="")

return '<br>' + 'F ' + Field("Fax");

else

return "";

Posted

Then update your current script with this.

 

if(Field("Mobile") !="")

return 'T '+Field("telephone") + "  |  M "+Field("Mobile") + Rule("JustFax_RULE");

else

return 'T '+Field("telephone") + "  |  F "+ Field ("Fax");

Posted

This is almost exactly the same question as in this thread:

http://forums.printable.com/showthread.php?t=1619

 

However, your requirements are a little more complicated, because the delimiters (separators) between the fields are not constant. Specifically, your first delimiter is " | ", and your second delimiter is "<br>". So the simple method which Eric suggested in that other thread of appending items to a result string as you go won't quite work.

 

Instead, I would use a variation on my more generalized solution from that other thread, which first puts each separate field and its label into an array. Then we can deal with things depending on how many non-empty items we end up with. Try this:

var Entries = [
   "T ", TaggedFromRaw(Field("telephone")),
   "M ", TaggedFromRaw(Field("Mobile")),
   "F ", TaggedFromRaw(Field("Fax")),
];

var items = [];
for (var i = 0; i < Entries.length; i += 2)
 if (Entries[i] && Entries[i+1])
   items.push(Entries[i] + Entries[i+1]);

return items.slice(0,2).join(" | ") + (items[2] ? "<br>" + items[2] : "");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...