&l_element3.0 form 验证">
当前位置:   article > 正文

vue3.0 实现element-form 表单校验和提交_element3.0 form 验证

element3.0 form 验证

vue3.0 实现element-form 表单校验和提交

vue3.0+element-plus的form表单:

<template>
    <el-container>
        <el-header>
            <Header title="注册" :showBack= "true"></Header>
        </el-header>
     <el-main>
         <el-form class="content" :model="ruleForm" :rules="rules" ref="myForm" label-width="120px" >
              <el-form-item class="name" label="手机号:" prop="name" >
                <el-input placeholder="请输入手机号"   v-model.number="ruleForm.name" clearable autocomplete="off"></el-input>
              </el-form-item>
                <el-form-item class="=passwords" label="密码:"  prop="password" >
                   <el-input placeholder="请输入密码" clearable  v-model="ruleForm.password"  show-password ></el-input>
                </el-form-item>
                <el-form-item class="=confirm" label="确认密码:"  prop="checkpass">
                   <el-input placeholder="请再次输入密码" clearable  v-model="ruleForm.checkpass" show-password ></el-input>
                </el-form-item>
                <el-form-item>
                     <el-button-group>
                         <el-button type="success" class="confirm-btn" @click="submitForm">确认</el-button>
                          <el-button type="info" class="cancel-btn" @click="resetForm">取消</el-button>
                    </el-button-group>
                </el-form-item>            
         </el-form>           
     </el-main>
    </el-container>
</template>
<script>
import Header from '../components/Header.vue'
import {  reactive, ref, unref } from 'vue'
import { useRouter } from 'vue-router'
export default ({
    components:{
        Header
    }, 
    setup(props, context) {
        const myForm = ref(null)
         const router = useRouter();
        const ruleForm = reactive({
            name:'',
            password:'',
            checkpass:''
        })
        const rules = {
            name: [
                { required: true, trigger: 'blur', validator: checkName, },
            ],
            password: [
                { required: true, trigger: 'blur', validator: validatePass },
            ],
            checkpass: [
                { required: true,  trigger: 'blur', validator: validatePass2 } 
            ]
            }
      function checkName (rule, value, callback) {
        if (!value) {
          return callback(new Error('不能为空'));
        }
        setTimeout(() => {
          var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
            if (!myreg.test(value)) {
              callback(new Error('请输入正确的手机号'));
            } else {
              callback();
            }
        }, 1000);
      };
      function  validatePass(rule, value, callback){
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (ruleForm.checkpass !== '') {
            ruleForm.validateField('checkpass');
          }
          callback();
        }
      }
       function validatePass2 (rule, value, callback) {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== ruleForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
        const submitForm = () => {
              const form = unref(myForm)
                try {
                 let res = form.validate()
                 if (res){
                        router.push({ name: 'Admin' })  
                   }   
                } catch (error){
                  alert('error submit!!')
                }               
            }
     function resetForm() {
          const form = unref(myForm)
           form.resetFields();
      }  
        return {
             myForm, ruleForm, rules, validatePass, validatePass2, checkName, submitForm, resetForm
        }
    }
})
</script>
<style scoped>
 .el-header{
    background-color: #080e16;
    color: rgb(241, 235, 235);
    text-align: center;
    line-height: 60px;
    font-size:24px;
  }
  .el-input{
    width: 10rem;
  }
  .content{
      padding-top: 100px;    
  }
  .el-button{
      font-size: 14px;
      height: 2rem;
      margin-top: 1rem;
  }
 
 
</style>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/280028
推荐阅读
相关标签