Skip to content

Instantly share code, notes, and snippets.

@zackpyle
Last active August 16, 2023 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zackpyle/bc0d2129596a6d5e47ebd394efabc002 to your computer and use it in GitHub Desktop.
Save zackpyle/bc0d2129596a6d5e47ebd394efabc002 to your computer and use it in GitHub Desktop.

Two options to format US phone numbers

Loops and Logic
Use Loops and Logic's regex feature using capture groups to grab the parts, then insert . or - when reassembling the capture groups

Javascript
Using JS to extract (slice) the phone number into separete parts, inject the . or - and reassemble

<!-- Format phone number with L&L regex -->
<Format replace_pattern="/(\d{3})(\d{3})(\d{4})/" with="$1.$2.$3"><Field phone_number /></Format>
<!-- An example of this in use -->
<a href="tel:{Field phone_number /}{If field=phone_extension};{Field phone_extension /}{/If}">
<span class="phone-number"><Format replace_pattern="/(\d{3})(\d{3})(\d{4})/" with="$1.$2.$3"><Field phone_number /></Format></span>
<If field="phone_extension">
<span class="phone-extension">ext.<Field phone_extension /></span>
</If>
</a>
jQuery(document).ready(function($) {
// Find all elements with class "phone-number"
$('.phone-number').each(function() {
// Get the phone number from the element
var phoneNumber = $(this).text().trim();
// Format the phone number
var formattedPhoneNumber = phoneNumber.slice(0, 3) + '.' + phoneNumber.slice(3, 6) + '.' + phoneNumber.slice(6);
// Update the content of the element with the formatted phone number
$(this).text(formattedPhoneNumber);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment