vue 表单校验 校验规则 自定义rules
input不能为空校验:this.codeRules['NAME'] = [{required: true,message: '姓名不能为空',trigger: 'blur|change'}]长度校验:this.codeRules['SFZH'] = [{required: true,message: '身份证号不能为空',trigger: 'blur|change'},
·
input不能为空校验:
this.codeRules['NAME'] = [
{
required: true,
message: '姓名不能为空',
trigger: 'blur|change'
}
]
长度校验:
this.codeRules['SFZH'] = [
{
required: true,
message: '身份证号不能为空',
trigger: 'blur|change'
},
{
type: 'string',
min: 18,
max: 18,
message: '长度为18个字符',
trigger: 'blur'
}
]
电话校验:
let validateMobile = (rule, value, callback) => {
let reg01 = /^(0|86|17951)?([1][3,4,5,7,8,9])[0-9]{9}$/
let reg02 = /^(0[0-9]{2,3}\-)([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$/
if (reg01.test(value) || reg02.test(value) || value === '') {
callback()
} else {
callback(
new Error("请填写正确的监督电话(手机号或以'-'隔开的座机号)")
)
}
}
this.codeRules['PHONE'] = [
{ required: true, message: '电话号码不能为空', trigger: 'blur' },
{ validator: validateMobile, trigger: 'blur' }
]
邮箱校验:
this.codeRules['EMAIL'] = [
{
required: true,
message: '邮箱不能为空',
trigger: 'blur'
},
{
type: 'email',
message: '请输入正确的邮箱地址',
trigger: 'blur|change'
}
]
统一社会信用代码校验:
// 判断统一社会信用代码
// 规则:由18位数字或大写拉丁字母组成,包括登记管理部门代码(1位)、机构类别代码(1位)、登记管理机关行政区划码(6位)、主体标识码(9位全国组织机构代码)、验码(1位)五个部分组成
export function CheckSocialCreditCode (Code) {
let patrn = /^[0-9A-Z]+$/ // 18位校验及大写校验
if (Code.length !== 18 || patrn.test(Code) === false) {
return false
} else {
let Ancode // 统一社会信用代码的每一个值
let Ancodevalue // 统一社会信用代码每一个值的权重
let total = 0
let weightedfactors = [
1,
3,
9,
27,
19,
26,
16,
17,
20,
29,
25,
13,
8,
24,
10,
30,
28
] // 加权因子
let str = '0123456789ABCDEFGHJKLMNPQRTUWXY' // 不用I、O、S、V、Z
for (let i = 0; i < Code.length - 1; i++) {
Ancode = Code.substring(i, i + 1)
Ancodevalue = str.indexOf(Ancode)
total = total + Ancodevalue * weightedfactors[i] // 权重与加权因子相乘之和
}
let logiccheckcode = 31 - (total % 31)
if (logiccheckcode === 31) {
logiccheckcode = 0
}
let Str =
'0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y'
// eslint-disable-next-line camelcase
let Array_Str = Str.split(',')
logiccheckcode = Array_Str[logiccheckcode]
let checkcode = Code.substring(17, 18)
if (logiccheckcode !== checkcode) {
console.log(123)
return false
}
return true
}
}
let validateCreditCode = (rule, value, callback) => {
if (!value) {
return callback(new Error('统一社会信用代码不能为空'))
}
setTimeout(() => {
if (CheckSocialCreditCode(value.trim()) === true) {
callback()
} else {
callback(new Error('请输入正确的统一社会信用代码'))
}
}, 100)
}
this.codeRules['creditCode'] = [
{
required: true,
message: '统一社会信用代码不能为空',
trigger: 'change'
},
{
validator: validateCreditCode,
trigger: 'blur'
}
]
身份证号校验:
let rulesIdNumber = (rule, value, callback) => {
if (!value) {
return new Error('请输入身份证号)')
} else {
const reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(([0-2][1-9])|10|20|30|31)\d{3}(\d|X|x)$/
const card = reg.test(value)
if (!card) {
callback(new Error('请输入正确的身份证号信息'))
} else {
callback()
}
}
}
this.codeRules['number'] = [
{
required: true,
message: '身份证号不能为空',
trigger: 'change'
},
{
validator: rulesIdNumber,
trigger: 'blur'
}
]
更多推荐
已为社区贡献2条内容
所有评论(0)