使用正则表达式
外部引入
将正则表达式方法写在publicMethod => index.js文件内
checkPhone: function (str) {//传入str
let isFixMob = /^0?1[0-9][0-9]\d{8}$/;//正则表达式
if (isFixMob.test(str) ) {//判断str是否符合正则表达式
return true;
} else {
return false;
}
}
并使用export default{} 暴露出来
export default{
checkPhone: function (str) {//传入str
let isFixMob = /^0?1[0-9][0-9]\d{8}$/;//正则表达式
if (isFixMob.test(str)) {//判断str是否符合正则表达式
return true;
} else {
return false;
}
}
}
在main.js中声明
import WAY from './publicMethod/index.js'
Vue.prototype.WAY = WAY
在其他页面中使用
this.WAY.checkPhone(传入的值);//符合返回true;否则为false;
内部使用
在methods内写正则表达式
data(){
return{
cellphone:''
}
},
methods:{
phone(){
if(this.checkPhone(this.cellphone)){
alert('合规')
}else{
alert('不合规')
}
},
checkPhone(str) {
let isFixMob = /^0?1[0-9][0-9]\d{8}$/;
if (isFixMob.test(str) ) {
return true;
} else {
return false;
}
},
}