uniapp中添加全局的检验

1 在项目根目录下新建文件夹和文件

 

 

const check = {
    telphone(data) {
        if(!(/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(data))){
            uni.showToast({
                title: "手机号码不正确",
                icon:'none'
            });     
            return false; 
        } 
        return true
    },
    password(data) {
        if(data.length<6) {
            uni.showToast({
                title: "密码长度不能小于6位",
                icon:'none'
            });   
            return false;  
        }
        return true
    }
    
}

export default check;

2 在main.js中导入和全局挂载一下

 

 3 在页面中使用

// html
<input placeholder="请填写有效的手机号码" @blur="inpValue()" v-model="phone" maxlength="11"></input>

// js
    inpValue() {
                if (this.$check.telphone(this.phone) == false) {
                    uni.showToast({
                        title: '手机号格式不正确',
                        icon: 'none'
                    })
                }
            },

 

posted @ 2021-11-28 22:17  这是一个寂寞的天  阅读(360)  评论(0)    收藏  举报