Vue element 自定义表单验证+手机号正则验证+用户名必须是中文

上图~~~

Vue element 自定义表单验证+手机号正则验证+用户名必须是中文_第1张图片


<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue element 自定义表单验证title>
head>
<style>
    @import url("//unpkg.com/element-plus/lib/theme-chalk/index.css");
style>
<body>
<script src="//unpkg.com/vue@next">script>
<script src="//unpkg.com/element-plus/lib/index.full.js">script>
<div id="app">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
            <el-input v-model.number="ruleForm.username">el-input>
        el-form-item>
        <el-form-item label="密码" prop="pass">
            <el-input type="password" v-model="ruleForm.pass" autocomplete="off">el-input>
        el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off">el-input>
        el-form-item>
        <el-form-item label="年龄" prop="age">
            <el-input v-model.number="ruleForm.age">el-input>
        el-form-item>
        <el-form-item label="手机号" prop="tel">
            <el-input v-model.number="ruleForm.tel">el-input>
        el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交el-button>
            <el-button @click="resetForm('ruleForm')">重置el-button>
        el-form-item>
    el-form>
div>
body>
html>
<script>
    var Main = {
            data() {
                //验证年龄
                var checkAge = (rule, value, callback) => {
                    if (!value) {
                        return callback(new Error('年龄不能为空'));
                    }
                    setTimeout(() => {
                        if (!Number.isInteger(value)) {
                            callback(new Error('请输入数字值'));
                        } else {
                            if (value < 18) {
                                callback(new Error('必须年满18岁'));
                            } else {
                                callback();
                            }
                        }
                    }, 1000);
                };
                //验证用户名
                var checkUserName = (rule, value, callback) => {
                    if (!value) {
                        return callback(new Error('用户名不能为空'));
                    } else {
                        const reg = /^[\u4E00-\u9FA5]+$/
                        console.log(reg.test(value));
                        if (reg.test(value)) {
                            callback();
                        } else {
                            return callback(new Error('只能输入中文'));
                        }
                    }
                };
                //验证手机号
                var checkTel = (rule, value, callback) =>{
                    if (!value) {
                        return callback(new Error('手机号不能为空'));
                    } else {
                        const reg = /^1[3|4|5|7|8][0-9]\d{8}$/
                        console.log(reg.test(value));
                        if (reg.test(value)) {
                            callback();
                        } else {
                            return callback(new Error('请输入正确的手机号'));
                        }
                    }
                };
                //验证密码
                var validatePass = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请输入密码'));
                    } else {
                        if (this.ruleForm.checkPass !== '') {
                            this.$refs.ruleForm.validateField('checkPass');
                        }
                        callback();
                    }
                };
                var validatePass2 = (rule, value, callback) => {
                    if (value === '') {
                        callback(new Error('请再次输入密码'));
                    } else if (value !== this.ruleForm.pass) {
                        callback(new Error('两次输入密码不一致!'));
                    } else {
                        callback();
                    }
                };
                return {
                    ruleForm: {
                        pass: '',
                        checkPass: '',
                        age: '',
                        tel:'',
                        username:''
                    },
                    rules: {
                        pass: [
                            { validator: validatePass, trigger: 'blur' }
                        ],
                        checkPass: [
                            { validator: validatePass2, trigger: 'blur' }
                        ],
                        age: [
                            { validator: checkAge, trigger: 'blur' }
                        ],
                        tel:[
                            { validator: checkTel, trigger: 'blur' }
                        ],
                        username:[
                            { validator: checkUserName, trigger: 'blur' }
                        ]
                    }
                };
            },
            methods: {
                submitForm(formName) {
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            alert('submit!');
                        } else {
                            console.log('error submit!!');
                            return false;
                        }
                    });
                },
                resetForm(formName) {
                    this.$refs[formName].resetFields();
                }
            }
        }
    ;const app = Vue.createApp(Main);
    app.use(ElementPlus);
    app.mount("#app")
script>

你可能感兴趣的:(vue,element,表单验证)