Last active
August 3, 2024 06:38
-
-
Save kkiernan/7475b615485344f2fdf3 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: 123-456-7890 => (123) 456-7890 | |
* | |
* @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'); | |
}); |
Wait, does this work? I didn't know v-model
allows you to add filter like that in Vue2
@pxwee5 yup, they brought back filters for Vue 2.
How would this work for two-way binding since that's been removed in 2.0?
I receive the following error and won't compile: SyntaxError: Assigning to rvalue
filters don't work on v-model any more.
Just use vue-the-mask. v-mask="['(###) ###-###']"
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or there is vue-cleave