-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
简单的表单验证 #33
Comments
/* eslint-disable eqeqeq */
/* eslint-disable no-redeclare */
/* eslint-disable default-case */
/**
* var rule = [
{ name: "nickname", checkType: "string", checkRule: "1,3", errorMsg: "姓名应为1-3个字符" },
{ name: "gender", checkType: "in", checkRule: "男,女", errorMsg: "请选择性别" },
{ name: 'pw_confirm', checkType: 'same', checkRule: this.form.pw, errorMsg: '密码输入不一致' },
// allowNull:true 为空时不校验,非空时校验checkType
{ name: 'mail', allowNull:true, checkType: 'email', errorMsg: '邮箱格式有误' }
//......
];
*/
export default {
error: '',
check: function(data, rule) {
for (var i = 0; i < rule.length; i++) {
if (!rule[i].checkType) {
return true
}
if (!rule[i].name) {
return true
}
if (!rule[i].errorMsg) {
return true
}
if (!data[rule[i].name]) {
// 允许为空, 但非空时要check
if (rule[i].allowNull) {
continue
} else {
this.error = rule[i].errorMsg
return false
}
}
switch (rule[i].checkType) {
case 'string':
var reg = new RegExp('^.{' + rule[i].checkRule + '}$')
if (!reg.test(data[rule[i].name])) {
this.error = rule[i].errorMsg
return false
}
break
case 'int':
var reg = new RegExp('^(-[1-9]|[1-9])[0-9]{' + rule[i].checkRule + '}$')
if (!reg.test(data[rule[i].name])) {
this.error = rule[i].errorMsg
return false
}
break
case 'between':
if (!this.isNumber(data[rule[i].name])) {
this.error = rule[i].errorMsg
return false
}
var minMax = rule[i].checkRule.split(',')
minMax[0] = Number(minMax[0])
minMax[1] = Number(minMax[1])
if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
this.error = rule[i].errorMsg
return false
}
break
case 'betweenD':
var reg = /^-?[1-9][0-9]?$/
if (!reg.test(data[rule[i].name])) {
this.error = rule[i].errorMsg
return false
}
var minMax = rule[i].checkRule.split(',')
minMax[0] = Number(minMax[0])
minMax[1] = Number(minMax[1])
if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
this.error = rule[i].errorMsg
return false
}
break
case 'betweenF':
var reg = /^-?[0-9][0-9]?.+[0-9]+$/
if (!reg.test(data[rule[i].name])) {
this.error = rule[i].errorMsg
return false
}
var minMax = rule[i].checkRule.split(',')
minMax[0] = Number(minMax[0])
minMax[1] = Number(minMax[1])
if (data[rule[i].name] > minMax[1] || data[rule[i].name] < minMax[0]) {
this.error = rule[i].errorMsg
return false
}
break
case 'same':
if (data[rule[i].name] != rule[i].checkRule) {
this.error = rule[i].errorMsg
return false
}
break
case 'notsame':
if (data[rule[i].name] == rule[i].checkRule) {
this.error = rule[i].errorMsg
return false
}
break
case 'email':
var reg = /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
if (!reg.test(data[rule[i].name])) {
this.error = rule[i].errorMsg
return false
}
break
case 'phoneno':
var reg = /^1[0-9]{10,10}$/
if (!reg.test(data[rule[i].name])) {
this.error = rule[i].errorMsg
return false
}
break
case 'zipcode':
var reg = /^[0-9]{6}$/
if (!reg.test(data[rule[i].name])) {
this.error = rule[i].errorMsg
return false
}
break
case 'reg':
var reg = new RegExp(rule[i].checkRule)
if (!reg.test(data[rule[i].name])) {
this.error = rule[i].errorMsg
return false
}
break
case 'in':
if (rule[i].checkRule.indexOf(data[rule[i].name]) == -1) {
this.error = rule[i].errorMsg
return false
}
break
case 'notnull':
if (data[rule[i].name] == null || data[rule[i].name].length < 1) {
this.error = rule[i].errorMsg
return false
}
break
}
}
return true
},
isNumber: function(checkVal) {
var reg = /^-?[1-9][0-9]?.?[0-9]*$/
return reg.test(checkVal)
}
} 使用 const rule = [
{ name: 'eventTitle', checkType: 'notnull', errorMsg: '事件主题不能为空' },
{ name: 'eventTitle', checkType: 'string', checkRule: '1,50', errorMsg: '事件主题不能超过50个字符' },
{ name: 'eventContent', checkType: 'notnull', errorMsg: '输入事件描述' },
{ name: 'eventContent', checkType: 'string', checkRule: '1,500', errorMsg: '事件描述不能超过500个字符' },
{ name: 'eventTypeId', checkType: 'notnull', errorMsg: '选择事件类型' },
{ name: 'eventScale', checkType: 'notnull', errorMsg: '选择事件规模' },
{ name: 'urgency', checkType: 'notnull', errorMsg: '选择紧急程度' },
{ name: 'eventAddress', checkType: 'notnull', errorMsg: '输入地址' },
{ name: 'eventAddress', checkType: 'string', checkRule: '1,50', errorMsg: '地址不能超过50个字符' }
// { name: 'imageList', checkType: 'notnull', errorMsg: '上传事件照片或视频' }
]
const data = {
eventTitle: this.eventTitle,
eventContent: this.eventContent,
eventTypeId: this.eventTypeSelectList[this.eventTypeSelectList.length - 1].eventId,
eventScale: this.eventScale,
urgency: this.urgency,
eventAddress: this.eventAddress,
imageList: this.imageList
}
// checker
if (!checker.check(data, rule)) {
console.log(checker.error)
return uni.showToast({
icon: 'none',
title: checker.error
})
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
贴一个内部写的简单验证js
The text was updated successfully, but these errors were encountered: