Skip to content

Instantly share code, notes, and snippets.

@kkiernan
Last active May 4, 2022 05:19
Show Gist options
  • Save kkiernan/7475b615485344f2fdf3 to your computer and use it in GitHub Desktop.
Save kkiernan/7475b615485344f2fdf3 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: 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');
});
@andy3520
Copy link

Hi, is there any library support this kind of format?
text box

@AlanGreyjoy
Copy link

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