对比一下单独引入表单规则和 写在页面的表单规则
没有外部引入表单规则
<!-- -->
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>
export default {
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(() => {
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: checkPhone, trigger: 'blur' }],
password: [{ validator: checkPassword, trigger: 'blur' }]
}
}
},
created () {
},
methods: {
loginIn () {
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;
}
.login-container {
border-radius: 15px;
background-clip: padding-box;
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 {
data () {
return {
loginForm: {
username: '',
password: ''
},
responseResult: [],
rules: {
username: [{ validator: rules.FormValidate.Form().validatePhone, trigger: 'blur' }],
password: [{ validator: rules.FormValidate.Form().validatePsdReg, trigger: 'blur' }]
}
}
},
created () {
},
methods: {
loginIn () {
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;
}
.login-container {
border-radius: 15px;
background-clip: padding-box;
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 () {}
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