Skip to content

Instantly share code, notes, and snippets.

@rlfrahm
Forked from kkiernan/index.html
Last active May 24, 2017 15:14
Show Gist options
  • Save rlfrahm/4693a4fb37134fa25756f9b66b3ea080 to your computer and use it in GitHub Desktop.
Save rlfrahm/4693a4fb37134fa25756f9b66b3ea080 to your computer and use it in GitHub Desktop.
A Vue.js filter that formats a phone number. Adapted for Vue.js 2.0
<div id="vue">
<input type="text"
name="home_phone"
class="form-control"
v-model="homePhone"
v-on:keyup="formatPhone()"
lazy>
</div>
/**
* Formats a phone number.
* Example: 123-456-7890 => (123) 456-7890
*
* @param {String} phone
* @return {Void}
*/
var v = new Vue({
el: '#vue',
data: {
homePhone: ''
}, methods: {
formatPhone: function() {
this.homePhone = this.homePhone.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