vue element-ui 单独引入表单规则 减少页面结构

对比一下单独引入表单规则和 写在页面的表单规则

没有外部引入表单规则

在这里插入图片描述

<!--  -->
import { callbackify } from 'util';
<template>
  <body id="poster">
    <el-form class="login-container"
     :rules="rules"
     ref="loginForm"
     :model="loginForm"
      label-position="left" label-width="0px">
      <h3 class="login_title">系统登录</h3>
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>

      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 100%;background: #505458;border: none" v-on:click="loginIn">登录</el-button>
      </el-form-item>
    </el-form>
  </body>
</template>

<script>

// import rules from '@/utils/InspectionRules.js'

export default {
  // name: 'Login',
  data () {
    // 自定义表单验证规则
    var checkPhone = (rule, value, callback) => {
      const phoneReg = /^1[3|4|5|7|8][0-9]{9}$/
      if (!value) {
        console.log(value)
        return callback(new Error('电话号码不能为空'))
      }
      setTimeout(() => {
      // Number.isInteger是es6验证数字是否为整数的方法,实际输入的数字总是识别成字符串
      // 所以在前面加了一个+实现隐式转换

        if (!Number.isInteger(+value)) {
          callback(new Error('请输入数字值'))
        } else {
          if (phoneReg.test(value)) {
            callback()
          } else {
            callback(new Error('电话号码格式不正确'))
          }
        }
      }, 100)
    }
    var checkPassword = (rule, value, callback) => {
      const ispassword = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{8,30}$/
      if (!value) {
        return callback(new Error('密码不能为空'))
      }
      setTimeout(() => {
        if (ispassword.test(value)) {
          callback()
        } else {
          callback(new Error('密码格式不对,必须包含数字,小写字母,大写字母,特殊符号'))
        }
      }, 1000)
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      responseResult: [],
      // 表单检验规则
      rules: {
        // username: [{ validator: rules.FormValidate.Form().validatePhone, trigger: 'blur' }],
        // password: [{ validator: rules.FormValidate.Form().validatePsdReg, trigger: 'blur' }],
        username: [{ validator: checkPhone, trigger: 'blur' }],
        password: [{ validator: checkPassword, trigger: 'blur' }]
      }
    }
  },
  created () {

  },
  methods: {
    loginIn () {
      // 调用一下 user 下的登陆方法 ,为什么这么调用 去看 vuex
      this.$store.dispatch('user/loginIn')
    }

  }
}
</script>

<style>
* {
  margin: 0px;
  padding: 0px;
}
#poster {
  background: url("../static/imgs/2.jpg") no-repeat;
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
  background-repeat: no-repeat;
}
body {
  margin: 0px;
  padding: 0px;
}
/* div盒子居中 背景色透明 */
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  /* margin: 90px auto; */
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  opacity: 0.8;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>

引入表单规则

在这里插入图片描述

<!--  -->
import { callbackify } from 'util';
<template>
  <body id="poster">
    <el-form class="login-container"
     :rules="rules"
     ref="loginForm"
     :model="loginForm"
      label-position="left" label-width="0px">
      <h3 class="login_title">系统登录</h3>
      <el-form-item prop="username">
        <el-input type="text" v-model="loginForm.username" auto-complete="off" placeholder="账号"></el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码"></el-input>
      </el-form-item>

      <el-form-item style="width: 100%">
        <el-button type="primary" style="width: 100%;background: #505458;border: none" v-on:click="loginIn">登录</el-button>
      </el-form-item>
    </el-form>
  </body>
</template>

<script>
// 引入表单验证规则
import rules from '@/utils/InspectionRules.js'

export default {
  // name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      responseResult: [],
      // 表单检验规则
      rules: {
        username: [{ validator: rules.FormValidate.Form().validatePhone, trigger: 'blur' }],
        password: [{ validator: rules.FormValidate.Form().validatePsdReg, trigger: 'blur' }]
        // username: [{ validator: checkPhone, trigger: 'blur' }],
        // password: [{ validator: checkPassword, trigger: 'blur' }]
      }
    }
  },
  created () {

  },
  methods: {
    loginIn () {
      // 调用一下 user 下的登陆方法 ,为什么这么调用 去看 vuex
      this.$store.dispatch('user/loginIn')
    }

  }
}
</script>

<style>
* {
  margin: 0px;
  padding: 0px;
}
#poster {
  background: url("../static/imgs/2.jpg") no-repeat;
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
  background-repeat: no-repeat;
}
body {
  margin: 0px;
  padding: 0px;
}
/* div盒子居中 背景色透明 */
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  /* margin: 90px auto; */
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #fff;
  opacity: 0.8;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>

引入的表单规则

在这里插入图片描述


// 账号
const codeReg = /^(?![0-9]*$)(?![a-zA-Z]*$)[a-zA-Z0-9]{6,20}$/
// 电话
const phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/

// 必须为数字
const numberReg = /^\d+$|^\d+[.]?\d+$/

// 密码
const passwordReg = /^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,20}$/

// 联系人
const contactsReg = /^[\u0391-\uFFE5A-Za-z]+$/

const regId = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/

const emailReg = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/

const FormValidate = (function () {
  function FormValidate () {}
  // From表单验证规则  可用于公用的校验部分
  FormValidate.Form = function () {
    return {
      // 账号的验证规则
      validateCode (rule, value, callback) {
        if (!value) {
          return callback(new Error('请输入账号'))
        }
        if (!codeReg.test(value)) {
          callback(new Error('账号必须为6-20位字母和数字组合'))
        } else {
          callback()
        }
      },

      // 只能数字的验证
      validateNumber (rule, value, callback) {
        if (value !== '') {
          if (!numberReg.test(value)) {
            callback(new Error('员工数量必须为数字'))
          } else {
            callback()
          }
        } else {
          callback()
        }
      },

      // 密码的验证
      validatePsdReg (rule, value, callback) {
        if (!value) {
          return callback(new Error('请输入密码'))
        }
        if (!passwordReg.test(value)) {
          callback(new Error('请输入6-20位英文字母、数字或者符号(除空格),且字母、数字和标点符号至少包含两种'))
        } else {
          callback()
        }
      },

      // 联系人
      validateContacts (rule, value, callback) {
        if (!value) {
          return callback(new Error('请输入联系人'))
        }
        if (!contactsReg.test(value)) {
          callback(new Error('联系人不可输入特殊字符'))
        } else {
          callback()
        }
      },

      // 邮箱的验证规则
      validateEmail (rule, value, callback) {
        if (value !== '') {
          if (!emailReg.test(value)) {
            callback(new Error('邮箱格式不正确'))
          } else {
            callback()
          }
        } else {
          callback()
        }
      },

      // 电话号码的验证
      validatePhone (rule, value, callback) {
        if (!value) {
          return callback(new Error('请输入手机号码'))
        }
        if (!phoneReg.test(value)) {
          callback(new Error('手机格式不正确'))
        } else {
          callback()
        }
      },

      // 身份证的验证规则
      ID (rule, value, callback) {
        if (!value) {
          return callback(new Error('身份证不能为空'))
        }
        if (!regId.test(value)) {
          callback(new Error('请输入正确的二代身份证号码'))
        } else {
          callback()
        }
      }
    }
  }

  return FormValidate
}())

exports.FormValidate = FormValidate



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值