Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Phone mask with Vue.js
import Vue from "vue"
Vue.directive('phone', {
bind(el) {
el.oninput = function(e) {
if (!e.isTrusted) {
return
}
const x = this.value.replace(/\D/g, '').match(/(\d{0,1})(\d{0,3})(\d{0,3})(\d{0,4})/)
if (!x[2] && x[1] !== '') {
this.value = x[1] === '8' ? x[1] : '8' + x[1]
} else {
this.value = !x[3] ? x[1] + x[2] : x[1] + '(' + x[2] + ') ' + x[3] + (x[4] ? '-' + x[4] : '')
}
}
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.