把项目里用到的一些比较常用的正则记录一下.
1.验证密码必须有数字,字母,特殊字符组成
var validatePasswordAcquaintance = (rule, value, callback) => {
var passwordreg = /(?=.*\d)(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}/;
if (!passwordreg.test(value)) {
callback(new Error("密码必须由数字、字母、特殊字符组合,请输入8-16位"));
} else {
callback();
}
2.验证用户名长度
var validateUsernameAcquaintance = (rule, value, callback) => {
var reg = /^[\w]{6,16}$/i;
if (reg.test(value)) {
callback();
} else {
callback(new Error("用户名长度不符合6-16位"));
}
};
3.验证电话号码
var validatePhoneAcquaintance = (rule, value, callback) => {
var reg = /^1[3456789]\d{9}$/;
if (!reg.test(value)) {
callback(new Error("请输入有效的手机号码"));
}
callback();
};
4.验证ip地址是否有效
var validateIpAcquaintance = (rule, value, callback) => {
var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
var iplist = value.split(",");
for (var i = 0; i < iplist.length; i++) {
if (!reg.test(iplist[i])) {
callback(new Error("请输入有效IP地址,多个IP以,分割"));
iplist = [];
}
}
callback();
};
5.身份证号码校验
var validateIdcardAcquaintance = (rule, value, callback) => {
if(!validateIdCard(value)){
callback(new Error("身份证号码不符合规范"));
}else{
callback();
}
};