-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="vue"> | |
<input type="text" | |
name="home_phone" | |
class="form-control" | |
v-model="homePhone" | |
v-on:keyup="formatPhone()" | |
lazy> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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