# Automatic Phone Number and Postal Code Field Formatting

**URL:** <https://community.hubspot.com/t/automatic-phone-number-and-postal-code-field-formatting/41802>\
**Category:** CMS Development\
**Tags:** professional\
**Created:** [August 4, 2021, 4:12pm UTC](https://community.hubspot.com/t/automatic-phone-number-and-postal-code-field-formatting/41802 "2021-08-04T16:12:59Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [August 5, 2021, 5:07pm UTC](https://community.hubspot.com/t/automatic-phone-number-and-postal-code-field-formatting/41802/3 "2021-08-05T17:07:18Z")

</div>

Hi @lait, you can use this function for the phone number

```lia-code-sample
function formatPhoneNumber(phoneNumberString) {
 var cleaned = ('' + phoneNumberString).replace(/\D/g, '');
 var match = cleaned.match(/^(\d{3})(\d{3})(\d{3})$/);
 if (match) {
 return '(' + match[1] + ') ' + match[2] + ' ' + match[3];
 }
 return null;
}
// example:
formatPhoneNumber(123456789)
// outputs "(123) 456 789"

```

For the post codes you can do this:

```lia-code-sample
function formatPostCode(postCode){
 return postCode.toUpperCase().replace('-',' ');
}
formatPostCode('s0s-0s0');
// outputs "S0S 0S0"

```

---

_[View the full topic](https://community.hubspot.com/t/automatic-phone-number-and-postal-code-field-formatting/41802)._
