Skip to content

Instantly share code, notes, and snippets.

@johanneslamers
Forked from kkiernan/index.html
Created September 22, 2016 11:36
Show Gist options
  • Save johanneslamers/57e4dc10c0a9205103e8aeaa8ebaf60e to your computer and use it in GitHub Desktop.
Save johanneslamers/57e4dc10c0a9205103e8aeaa8ebaf60e to your computer and use it in GitHub Desktop.
A Vue.js filter that formats a phone number.
<input type="text"
name="home_phone"
class="form-control"
v-model="homePhone | phone"
lazy>
/**
* Formats a phone number.
* Example: 610-401-4565 => (610) 401-4565
*
* @param {String} phone
* @return {Void}
*/
Vue.filter('phone', function (phone) {
return phone.replace(/[^0-9]/g, '')
.replace(/(\d{3})(\d{3})(\d{4})/, '($1) $2-$3');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment