记录一下平时用到的一些正则表达式
1.最多保留一位小数,整数不限制:
^(([1-9]{1}\d*)|(0{1}))(\.\d{0,1})?$
2.最多保留两位整数,一位小数:
^\d{1,2}(\.\d{0,1})?$
3.限制只能以8或者9开头的五位数字:
[8-9]\d{4}$
4.校验是否包含非法字符:
/^[^@\/\'\\\"#$%&\^\*]+$/
5.校验是否包含空格:
例如:下面是vue与el-form,在data中定义了一个自定义的校验方法
data() {
let check = (rule, Value, cb) => {
if (Value != null && Value !== '') {
if (Value.indexOf(' ') !== -1) {
cb(new Error('不能包含空格'))
} else {
// 不能包含非法字符
const reg = /^[^@\/\'\\\"#$%&\^\*]+$/;
if (reg.test(Value)) {
return cb();
}
cb(new Error("不能包含非法字符"));
}
} else {
cb()
}
};
}
使用:
rules: {
code: [
{
required: true,
trigger: ["blur"]
},
{ validator: Code, trigger: "blur" }
],
}
持续更新中。。。