Vue项目elementUI中封装表单验证

1.直接在Vue文件中使用验证:

<template>
    <div class="login-wrap">
        <div class="ms-title">后台管理系统</div>
        <div class="ms-login">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="password" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </div>
                <p style="font-size:12px;line-height:30px;color:#999;">Tips : 用户名和密码随便填。</p>
            </el-form>
        </div>
    </div>
</template>

<script>
export default {
  data: function() {
    let checkName = (rule, value, cb) => {
        this.$store.commit('checkName',{rule,value,cb});
    };
    return {
      ruleForm: {
        username: "admin",
        password: "123123"
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { validator: checkName }
          // { min:10 , message:'最少10个字符',trigger:'blur'},
          // { pattern: /^[\u4E00-\u9FA5]+$/, message:'用户名只能是中文'},
          // { pattern:/^[a-zA-Z]w{5,17}$/, message: '以字母开头,长度在6-18之间, 只能包含字符、数字和下划线'}
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          localStorage.setItem("ms_username", this.ruleForm.username);
          this.$router.push("/");
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

2. 使用vuex将需要用到的表单验证写在mutations.js文件中

这需要安装vuex,步骤是:

  • cnpm i vuex --save
  • store/index.js
    import Vue from 'vue';
    import Vuex from 'vuex';
    import state from './state';
    import mutations from './mutations';
    import actions from './actions';
    Vue.use(Vuex);
    const store = new Vuex.Store({
        state,
        mutations,
        actions
    });
    export default store;

     

  • store/mutations_type.js
export const CHECK_NAME = 'checkName';
  • store/mutations.js
import {
    CHECK_NAME
} from './mutations_type';
export default {
    [CHECK_NAME](state , obj){
        if(!obj.value){
            obj.cb(new Error('请输入用户名'));
        }else if(!/^[\u4E00-\u9FA5]+$/.test(obj.value)){
            obj.cb(new Error('用户名只能是中文'));
        }else{
            obj.cb();
        }
    }
}
  •  将vuex引入main.js 
  • import store from './store/index';
    Vue.prototype.$store = store;

     

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值