提交 | 用户 | age
|
3ac5f2
|
1 |
/** |
J |
2 |
* elementUI 表单验证 |
|
3 |
*/ |
|
4 |
const validators = { |
|
5 |
required: { |
|
6 |
rule: /.+/, |
|
7 |
msg: '必填项不能为空' |
|
8 |
}, |
|
9 |
phone: { |
|
10 |
rule: /^[1][3,4,5,6,7,8][0-9]{9}$/, |
|
11 |
msg: '手机号格式不正确' |
|
12 |
}, |
|
13 |
mail: { |
|
14 |
rule: /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/, |
|
15 |
msg: 'E-mail格式不正确' |
|
16 |
}, |
|
17 |
id_card: { |
|
18 |
rule: /(^\d{18}$)|(^\d{17}(\d|X|x)$)|(^\d{15}$)/, |
|
19 |
msg: '身份证格式不正确' |
|
20 |
}, |
|
21 |
password: { |
|
22 |
rule: /^\d{6}$/, |
|
23 |
msg: '密码必须为6位数字' |
|
24 |
}, |
|
25 |
code: { |
|
26 |
rule: /^\d{4}$/, |
|
27 |
msg: '验证码格式不正确' |
|
28 |
}, |
|
29 |
number: { |
|
30 |
rule: /^\d+$/, |
|
31 |
msg: '必须为整数' |
|
32 |
}, |
|
33 |
digit: { |
|
34 |
rule: /^\d+(\.\d+)?$/, |
|
35 |
msg: '必须为数值' |
|
36 |
}, |
|
37 |
same: { |
|
38 |
rule (val='', sVal='') { |
|
39 |
return val===this.data[sVal] |
|
40 |
}, |
|
41 |
msg: '密码不一致' |
|
42 |
}, |
|
43 |
// https://blog.csdn.net/xjun0812/article/details/81806118 |
|
44 |
carNo: { |
|
45 |
// 含新能源车 |
|
46 |
rule: /^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/, |
|
47 |
msg: '车牌号格式不正确' |
|
48 |
}, |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* 验证手机号 |
|
53 |
* @param {string} options.trigger 触发事件 |
|
54 |
*/ |
|
55 |
function validatorTel(options = {}){ |
|
56 |
return [ |
|
57 |
{ |
|
58 |
validator: (rule, value, callback)=>{ |
|
59 |
if (!value) { |
|
60 |
return callback(new Error('请输入手机号码')) |
|
61 |
} |
|
62 |
if (!validators.phone.rule.test(value)) { |
|
63 |
return callback(new Error('请输入正确格式手机号')) |
|
64 |
} |
|
65 |
callback() |
|
66 |
}, |
|
67 |
trigger: options.trigger || 'blur' |
|
68 |
} |
|
69 |
] |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* 验证电子邮箱 |
|
74 |
* @param {string} options.trigger 触发事件 |
|
75 |
*/ |
|
76 |
function validatorEmail(options = {}){ |
|
77 |
return [ |
|
78 |
{ |
|
79 |
validator: (rule, value, callback) => { |
|
80 |
if (!value) { |
|
81 |
return callback(new Error('请输入电子邮箱')) |
|
82 |
} |
|
83 |
if (!validators.mail.rule.test(value)) { |
|
84 |
return callback(new Error('请输入正确格式电子邮箱')) |
|
85 |
} |
|
86 |
callback() |
|
87 |
}, |
|
88 |
trigger: options.trigger || 'blur' |
|
89 |
} |
|
90 |
] |
|
91 |
} |
|
92 |
|
|
93 |
/** |
|
94 |
* 验证非空 |
|
95 |
* @param {object} options 选项 |
|
96 |
* @param {string} options.message 提示 |
|
97 |
* @param {string} options.trigger 触发事件 |
|
98 |
*/ |
|
99 |
function validatorRequired(options = {}){ |
|
100 |
return [{ |
|
101 |
validator: (rule, value, callback) => { |
|
102 |
// =='' 可以兼容数组为空的情况 |
|
103 |
if (value=='') { |
|
104 |
return callback(new Error(options.message || validators.required.msg)) |
|
105 |
} |
|
106 |
callback() |
|
107 |
}, |
|
108 |
trigger: options.trigger || 'blur' |
|
109 |
}] |
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* 验证密码 |
|
114 |
* @param {object} options |
|
115 |
* @param {string} options.message 可选,提示文本 |
|
116 |
* @param {function} options.beforeCallback 可选,用于检查确认密码 |
|
117 |
* @param {string} optionss.trigger 触发事件 |
|
118 |
*/ |
|
119 |
function validatorPassword(options = {}){ |
|
120 |
return [{ |
|
121 |
validator: (rule, value, callback)=>{ |
|
122 |
if (!value) { |
|
123 |
return callback(new Error(options.message || '请输入密码')) |
|
124 |
} |
|
125 |
// 预留,用于检查确认密码 |
|
126 |
typeof options.beforeCallback === 'function' && options.beforeCallback() |
|
127 |
callback() |
|
128 |
}, |
|
129 |
trigger: options.trigger || 'blur' |
|
130 |
}] |
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* 验证确认密码 |
|
135 |
* @param {object} options |
|
136 |
* @param {function} options.password 必须,用于获取密码 |
|
137 |
* @param {string} optionss.trigger 触发事件 |
|
138 |
*/ |
|
139 |
function validatorConfirmPwd(options = {}){ |
|
140 |
return [{ |
|
141 |
validator: (rule, value, callback)=>{ |
|
142 |
// 动态获取密码 |
|
143 |
var password |
|
144 |
if (typeof options.password === 'function') { |
|
145 |
password = options.password() |
|
146 |
} else { |
|
147 |
password = options.password |
|
148 |
} |
|
149 |
|
|
150 |
if (!value) { |
|
151 |
callback(new Error('请输入确认密码')) |
|
152 |
} else if (value !== password) { |
|
153 |
callback (new Error('两次输入密码不一致!')) |
|
154 |
} else{ |
|
155 |
callback() |
|
156 |
} |
|
157 |
}, |
|
158 |
trigger: options.trigger || 'blur' |
|
159 |
}] |
|
160 |
} |
|
161 |
|
|
162 |
export default { |
|
163 |
validators, |
|
164 |
validatorTel, |
|
165 |
validatorEmail, |
|
166 |
validatorRequired, |
|
167 |
validatorPassword, |
|
168 |
validatorConfirmPwd, |
|
169 |
} |