-
-
Save johanneslamers/57e4dc10c0a9205103e8aeaa8ebaf60e to your computer and use it in GitHub Desktop.
A Vue.js filter that formats a phone number.
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
<input type="text" | |
name="home_phone" | |
class="form-control" | |
v-model="homePhone | phone" | |
lazy> |
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: 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