validate.js
950 Bytes
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal (path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
// ex: {validator:validateCode,trigger:'blur'}
// 验证code
export const validateCode = (rule, value, callback) => {
const reg = /^[A-Z]{1}[A-Za-z0-9]*$/
!reg.test(value) ? callback('字母开头、数字和字母组成') : callback()
}
// 验证 网址
export const validateUrl = (rule, value, callback) => {
const reg = /^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/
!reg.test(value) ? callback('URL格式不正确') : callback()
}
// 验证 电话
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()
}
}