vue项目中常用正则大全
<el-input v-model="ruleForm" @input="onRegularVerification($event)" />
export default {
methods: {
onRegularVerification(val) {
this.ruleForm = verifyNumberIntegerAndFloat(val);
}
}
}
1.小数或整数(不可以负数)
function verifyNumberIntegerAndFloat(val) {
let v = val.replace(/(^\s*)|(\s*$)/g, '');
v = v.replace(/[^\d.]/g, '');
v = v.replace(/^0{2}$/g, '0');
v = v.replace(/^\./g, '');
v = v.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
v = v.replace(/^(\\-)*(\d+)\.(\d\d).*$/, '$1$2.$3');
return v;
}
2.正整数验证
function verifiyNumberInteger(val) {
let v = val.replace(/(^\s*)|(\s*$)/g, '');
v = v.replace(/[\\.]*/g, '');
v = v.replace(/(^0[\d]*)$/g, '0');
v = v.replace(/^0\d$/g, '0');
v = v.replace(/[^\d]/g, '');
return v;
}
3.匹配中文及空格
function verifyCnAndSpace(val) {
let v = val.replace(/[\u4e00-\u9fa5\s]+/g, '');
v = v.replace(/(^\s*)|(\s*$)/g, '');
return v;
}
4.匹配英文与空格
function verifyEnAndSpace(val) {
let v = val.replace(/[a-zA-Z]+/g, '');
v = v.replace(/(^\s*)|(\s*$)/g, '');
return v;
}
5.禁止输入空格
function verifyAndSpace(val) {
let v = val.replace(/(^\s*)|(\s*$)/g, '');
return v;
}
6.金额用 ,
区分开
function verifyNumberComma(val) {
let v = verifyNumberIntegerAndFloat(val);
v = v.toString().split('.');
v[0] = v[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
v = v.join('.');
return v;
}
7.匹配文字变色(搜索时)
function verifyTextColor(val, text = '', color = 'red') {
let v = text.replace(new RegExp(val, 'gi'), `${val}`);
return v;
}
8.数字转中文大写
function verifyNumberCnUppercase(val, unit = '仟佰拾亿仟佰拾万仟佰拾元角分', v = '') {
val += '00';
let lookup = val.indexOf('.');
if (lookup >= 0) val = val.substring(0, lookup) + val.substr(lookup + 1, 2);
unit = unit.substr(unit.length - val.length);
for (let i = 0; i < val.length; i++) {
v += '零壹贰叁肆伍陆柒捌玖'.substr(val.substr(i, 1), 1) + unit.substr(i, 1);
}
v = v
.replace(/零角零分$/, '整')
.replace(/零[仟佰拾]/g, '零')
.replace(/零{2,}/g, '零')
.replace(/零([亿|万])/g, '$1')
.replace(/零+元/, '元')
.replace(/亿零{0,3}万/, '亿')
.replace(/^元/, '零元');
return v;
}
9.手机号码校验
function verifyPhone(val) {
if (!/^((12[0-9])|(13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/.test(val)) return false;
else return true;
}
10.国内电话号码
function verifyTelPhone(val) {
if (!/\d{3}-\d{8}|\d{4}-\d{7}/.test(val)) return false;
else return true;
}
11. 登录账号 (字母开头,允许5-16字节,允许字母数字下划线)
function verifyAccount(val) {
if (!/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/.test(val)) return false;
else return true;
}
12.密码 (以字母开头,长度在6~16之间,只能包含字母、数字和下划线)
function verifyPassword(val) {
if (!/^[a-zA-Z]\w{5,15}$/.test(val)) return false;
else return true;
}
13.强密码 (字母+数字+特殊字符,长度在6-16之间)
function verifyPasswordPowerful(val) {
if (
!/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&\\.*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&\\.*]+$)(?![\d!@#$%^&\\.*]+$)[a-zA-Z\d!@#$%^&\\.*]{6,16}$/.test(val)
)
return false;
else return true;
}
14.密码强度
function verifyPasswordStrength(val) {
let v = '';
if (/^(?:\d+|[a-zA-Z]+|[!@#$%^&\\.*]+){6,16}$/.test(val)) v = '弱';
if (/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&\\.*]+$)[a-zA-Z\d!@#$%^&\\.*]{6,16}$/.test(val)) v = '中';
if (
/^(?![a-zA-z]+$)(?!\d+$)(?![!@#$%^&\\.*]+$)(?![a-zA-z\d]+$)(?![a-zA-z!@#$%^&\\.*]+$)(?![\d!@#$%^&\\.*]+$)[a-zA-Z\d!@#$%^&\\.*]{6,16}$/.test(val)
)
v = '强';
return v;
}
15.IP地址
function verifyIPAddress(val) {
if (
!/^(\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])$/.test(
val
)
)
return false;
else return true;
}
16.邮箱
function verifyEmail(val) {
if (
!/^(([^<>()\\[\]\\.,;:\s@"]+(\.[^<>()\\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(
val
)
)
return false;
else return true;
}
17.身份证
function verifyIdCard(val) {
if (!/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(val)) return false;
else return true;
}
18. 姓名
function verifyFullName(val) {
if (!/^[\u4e00-\u9fa5]{1,6}(·[\u4e00-\u9fa5]{1,6}){0,2}$/.test(val)) return false;
else return true;
}
19. 邮政编码
function verifyPostalCode(val) {
if (!/^[1-9][0-9]{5}$/.test(val)) return false;
else return true;
}
20. url
function verifyUrl(val) {
if (
!/^(?:(?:(?:https?|ftp):)?\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})).?)(?::\d{2,5})?(?:[/?#]\S*)?$/i.test(
val
)
)
return false;
else return true;
}
21. 车牌号
function verifyCarNum(val) {
if (
!/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(
val
)
)
return false;
else return true;
}
22. 验证百分比(不可以小数)
function verifyNumberPercentage(val) {
let v = val.replace(/(^\s*)|(\s*$)/g, '');
v = v.replace(/[^\d]/g, '');
v = v.replace(/^0/g, '');
v = v.replace(/^[1-9]\d\d{1,3}$/, '100');
return v;
}
23. 验证百分比(可以小数)
export function verifyNumberPercentageFloat(val) {
let v = verifyNumberIntegerAndFloat(val);
v = v.replace(/^[1-9]\d\d{1,3}$/, '100');
v = v.replace(/^100\.$/, '100');
return v;
}