validate.js
1.31 KB
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
/**
* @description: isExternal
* @param {string} path
* @returns {Boolean}
*/
export function isExternal (path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
// ex: {validator:validateCode,trigger:'blur'}
// 验证code
/**
* @description: 验证code
* @param {*} rule
* @param {*} value
* @param {*} callback
* @author: renchao
*/
export const validateCode = (rule, value, callback) => {
const reg = /^[A-Z]{1}[A-Za-z0-9]*$/
!reg.test(value) ? callback('字母开头、数字和字母组成') : callback()
}
// 验证 网址
/**
* @description: 验证 网址
* @param {*} rule
* @param {*} value
* @param {*} callback
* @author: renchao
*/
export const validateUrl = (rule, value, callback) => {
const reg = /^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/
!reg.test(value) ? callback('URL格式不正确') : callback()
}
// 验证 电话
/**
* @description: 验证 电话
* @param {*} rule
* @param {*} value
* @param {*} callback
* @author: renchao
*/
export const validatePhone = (rule, value, callback) => {
const phoneReg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/
const rel = /^[0]\d{2,3}-[1-9]\d{7}$/
if (!value) {
callback()
} else {
!(phoneReg.test(value) || rel.test(value))
? callback(new Error('请输入正确的联系电话'))
: callback()
}
}