星油云站
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

153 lines
5.6 KiB

import Vue from 'vue'
import VeeValidate from 'vee-validate'
Vue.use(VeeValidate)
const formatFileSize = function (size) {
let units = ['Byte', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
let threshold = 1024
size = Number(size) * threshold
let i = size === 0 ? 0 : Math.floor(Math.log(size) / Math.log(threshold))
return (size / Math.pow(threshold, i)).toFixed(2) * 1 + ' ' + units[i]
}
const fieldName = name // 原来的i18n的fieldName会显示绑定的name值,就是英文,实际项目中不需要
VeeValidate.Validator.localize('zh_CN', {
name: 'zh_CN',
attributes: {},
messages: {
required: () => `${fieldName}不能为空`,
_default: () => `无效${fieldName}`,
after: (field, [target]) => `${fieldName}必须在${target}之后`,
alpha_dash: () => `${fieldName}能够包含字母数字字符、破折号和下划线`,
alpha_num: () => `${fieldName}只能包含字母数字字符`,
alpha_spaces: () => `${fieldName}只能包含字母字符和空格`,
alpha: () => `${fieldName}只能包含字母字符`,
before: (field, [target]) => `${fieldName}必须在${target}之前`,
between: (field, [min, max]) => `${fieldName}必须在${min}${max}之间`,
confirmed: (field, [confirmedField]) =>
`${fieldName}不能和${confirmedField}匹配`,
credit_card: () => `${fieldName}格式错误`,
date_between: (field, [min, max]) => `${fieldName}必须在${min}${max}之间`,
date_format: (field, [format]) => `${fieldName}必须符合${format}格式`,
decimal: (field, [decimals = '*'] = []) =>
`${fieldName}必须是数字,且能够保留${
decimals === '*' ? '' : decimals
}位小数`,
digits: (field, [length]) =>
`${fieldName}必须是数字,且精确到${length}位数`,
dimensions: (field, [width, height]) =>
`${fieldName}必须在${width}像素与${height}像素之间`,
email: () => `${fieldName}不是一个有效的邮箱`,
ext: () => `${fieldName}不是一个有效的文件`,
image: () => `${fieldName}不是一张有效的图片`,
included: () => `${fieldName}不是一个有效值`,
integer: () => `${fieldName}必须是整数`,
ip: () => `${fieldName}不是一个有效的地址`,
length: (field, [length, max]) => {
if (max) {
return `${fieldName}长度必须在${length}${max}之间`
}
return `${fieldName}长度必须为${length}`
},
max: (field, [length]) => `${fieldName}不能超过${length}个字符`,
max_value: (field, [max]) => `${fieldName}必须小于或等于${max}`,
mimes: () => `${fieldName}不是一个有效的文件类型`,
min: (field, [length]) => `${fieldName}必须至少有${length}个字符`,
min_value: (field, [min]) => `${fieldName}必须大于或等于${min}`,
excluded: () => `${fieldName}不是一个有效值`,
numeric: () => `${fieldName}只能包含数字字符`,
regex: () => `${fieldName}格式无效`,
size: (field, [size]) => `${fieldName}必须小于${formatFileSize(size)}`,
url: () => `${fieldName}不是一个有效的url`
}
})
// 手机号码验证
VeeValidate.Validator.extend('mobile', {
getMessage: () => `请输入正确的手机号码`,
validate: value =>
/^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(17[0-9]{1})|(18[0-9]{1})|(19[0-9]{1})|(16[0-9]{1}))+\d{8})$/.test(
value
)
})
// 8-16 字母数字组合起来的密码
// 不能全数字不能全字母
VeeValidate.Validator.extend('pwdRule', {
getMessage: () => `请输入8-16数字与字母混合密码`,
validate: value =>
value.length >= 8 &&
/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/.test(
value
)
})
// 短信验证码验证
// 验证个屁并不知道格式
VeeValidate.Validator.extend('random', {
getMessage: () => `请输入正确的验证码`,
validate: value =>
value.length === 6 &&
/^\d{6}$/.test(
value
)
})
// 汉语姓名验证
// 支持少数民族名字
VeeValidate.Validator.extend('userName', {
getMessage: () => `请输入正确的姓名`,
validate: value =>
/^[\u4e00-\u9fa5]+(·[\u4e00-\u9fa5]+)*$/.test(
value
)
})
// 15位或18位身份证号验证
VeeValidate.Validator.extend('idNumber', {
getMessage: () => `请输入有效的身份证号`,
validate: value =>
/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/
.test(
value
)
})
// 19位道路运输资格证号
VeeValidate.Validator.extend('eqcNumber', {
getMessage: () => `请输入正确的19位道路运输资格证号`,
validate: value =>
value.length === 19 &&
/^\d{19}$/.test(
value
)
})
// 车牌号验证
VeeValidate.Validator.extend('plateRule', {
getMessage: () => `请输入正确的车牌号`,
validate: value =>
/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][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挂学警港澳使领]))$/
.test(
value
)
})
// 银行卡号格式验证
VeeValidate.Validator.extend('bankCardRule', {
getMessage: () => `请输入有效的银行卡号`,
validate: value =>
/^([1-9]{1})(\d{14}|\d{15}|\d{18})$/.test(
value
)
})
// 8-16 字母数字组合起来的密码
// 不能全数字不能全字母
VeeValidate.Validator.extend('pwdRule', {
getMessage: () => `请输入8-16数字与字母混合密码`,
validate: value =>
value.length >= 8 &&
/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/.test(
value
)
})