Jump to content

Change Phone Number format on Report


Recommended Posts

Hi there,

If you ever faced with the question to format the phone number in Caspio, you probably know that there is a solution to input the phone number in the needed format.
But what if you already have a table with phone numbers in the format, like '0123456789', how would you outpu on the Tabular Report in the format '0123-456-789'?

That's the question I faced and because of that I decided to write a small JavaScript that I want to share with you hoping it can be useful:
 

<script>
document.addEventListener('DataPageReady', function (event) {
let columnNumber = 1;
let numbers = document.querySelectorAll(`.cbResultSetOddRow > td:nth-child(${columnNumber}), .cbResultSetEvenRow td:nth-child(${columnNumber})`)
for (let number of numbers) {   
  let numberText = number.innerText;
  let cleaned = ('' + numberText).replace(/\D/g, '');
   if (cleaned.length == 10) {
  let match = cleaned.match(/^(\d{3})(\d{3})(\d{4})$/);
  number.textContent = match[1] + '-' + match[2] + '-' + match[3];
   }
}
});
</script>

You can insert the code into HTML block, Header or Footer.
Also, on the 3rd line 'let columnNumber = 1;' you need to change that number to the number of the column where you have your phone numbers.

If you wish to change it to another format or to use it in another type of report and you do not know how to use that script, please let me know.

Link to comment
Share on other sites

  • 1 year later...

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.

Loading...
×
×
  • Create New...