1.方式一
el-from :rules=“rules”
el-from-item 中prop与rules中属性名对应
rules: {
name:[{
required: true,
message: '请输入用户名',
trigger: 'blur'
},{
min: 2,
max: 5,
message: '长度在 2 到 5 个字符'
},{
pattern: /^[\u4E00-\u9FA5]+$/,
message: '用户名只能为中文'
}
//{ pattern:/^[a-zA-Z]w{1,4}$/, message: '以字母开头,长度在2-5之间, 只能包含字符、数字和下划线'}
],
password: [{
required: true,
message: '请输入密码',
trigger: 'blur'
}, {
min: 6,
max: 30,
message: '长度在 6 到 30 个字符'
}, {
pattern: /^(\w){6,20}$/,
message: '只能输入6-20个字母、数字、下划线'
}],
mobile:[{
required: true,
message: '请输入手机号码',
trigger: 'blur'
},
{validator:function(rule,value,callback){
if(/^1[34578]\d{9}$/.test(value) == false){
callback(new Error("请输入正确的手机号"));
}else{
callback();
}
}, trigger: 'blur'}
],
// pattern: /^1[34578]\d{9}$/, message: '目前只支持中国大陆的手机号码' }
peopleID:[{
required: true,
message: '请输入身份证ID',
trigger: 'blur'
},{
pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message: '你的身份证格式不正确'
}
],
carId:[
{required: true, message: '请输入车牌号', trigger: 'blur'},
{pattern:/(^[\u4E00-\u9FA5]{1}[A-Z0-9]{6}$)|(^[A-Z]{2}[A-Z0-9]{2}[A-Z0-9\u4E00-\u9FA5]{1}[A-Z0-9]{4}$)|(^[\u4E00-\u9FA5]{1}[A-Z0-9]{5}[挂学警军港澳]{1}$)|(^[A-Z]{2}[0-9]{5}$)|(^(08|38){1}[A-Z0-9]{4}[A-Z0-9挂学警军港澳]{1}$)/,
message: '常规格式:晋B12345'},
],
},
1.方式二
行内校验 直接:rules写需求
<el-form-item
:label="$t('user.regionname')"
prop="regionids"
:rules="[
{ required: true, message: 'The regionname cannot be empty',trigger:'blur'}
]">
<el-input id="regionids" style="width:80%;" readonly name="regionids" :title="update.regionids" v-model="update.regionids"/>
<RegionSelect v-bind:genre="3" @selectRegion="getSelRegion" v-bind:toChildName="dialogStatus==='update'?update.regionids:fromChildNames"></RegionSelect>
</el-form-item>
3.方式三
自定义校验 同方式一 只是将其抽取出来 写成函数
rule为校验规则对象
value为 输入值
callback为回调函数 用于返回
data() {
const validateUsername = (rule, value, callback) => {
if(!value || value == ''){
callback(new Error('用户名不能为空'))
}else {
callback()
}
}
const validatePass = (rule, value, callback) => {
if(!value || value == ''){
callback(new Error('密码不能为空'))
}else if (value.length < 5 || value.length > 25) {
callback(new Error('密码长度为5-25个字符'))
} else {
callback()
}
}
return {
user:{},
loginRules: {
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
password: [{ required: true, trigger: 'blur', validator: validatePass }]
},
loading: false,
pwdType: 'password',
redirect: undefined
}
}
网友评论