elementUI中的el-form常用校验规则:
校验使用方式:
javascript
rules: {
name: [
{ required: true, message: '请输入活动名称', trigger: 'blur' },
{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
],
region: [
{ required: true, message: '请选择活动区域', trigger: 'change' }
],
date1: [
{ type: 'date', required: true, message: '请选择日期', trigger: 'change' }
],
date2: [
{ type: 'date', required: true, message: '请选择时间', trigger: 'change' }
],
type: [
{ type: 'array', required: true, message: '请至少选择一个活动性质', trigger: 'change' }
],
resource: [
{ required: true, message: '请选择活动资源', trigger: 'change' }
],
desc: [
{ required: true, message: '请填写活动形式', trigger: 'blur' }
]
}
};
常用的校验规则:
1、不能为空
javascript
{ required: true, message: '请填写活动形式', trigger: 'blur' }
2、限制字符串长度
javascript
{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
3、只能是正整数
javascript
{
validator: (rule, value, callback) => {
if (/^(?:[1-9]\d*)$/.test(value) == false) {
callback(new Error("请输入正整数"));
} else {
callback();
}
},
trigger: "change",
},
4、只能是数字
javascript
{ type: 'number', message: '必须为数字类型' },
5、数字必须大于18
javascript
{
validator: (rule, value, callback) => {
if (value < 18) {
callback(new Error('年龄必须超过18岁'))
} else {
callback()
}
},
trigger: ['blur', 'change']
},