我的项目中有多出需要输入手机号,我封装mixins里telphone.js,控制手机号344展示格式,代码如下:
import {
handlerPhone } from '@utils/Reg';
export const telphone = {
data(){
return {
threephone: '',
userInfo:{
phone
}
}
},
watch: {
threephone (newValue, oldValue) {
// 监听电话号码
// 方法一
this.threephone = handlerPhone(newValue,oldValue)
// 方法二
// this.threephone = newValue.length > oldValue.length ? newValue.replace(/\D/g,'').replace(/\s/g, '').replace(/(\d{3})(\d{0,4})(\d{0,4})/, '$1 $2 $3') : this.threephone.trim()
if (this.threephone.length === 13) {
// 验证/保存的手机号码,去除空格
this.userInfo.phone = this.threephone.replace(/\s/g, '')
// console.log('输入的电话号码是:', this.userInfo.phone)
}
}
},
}
检验手机号是否合法 写在Reg.js
// 验证手机号