vue正则,各种正则表达式
extend("email", value => {
let re = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
if (!re.test(value)) {
return "请输入正确的{_field_}";
} else if (value.length > 50) {
return "{_field_}不能超过50个字符";
} else {
return true;
}
});
extend("confirmed", {
...confirmed,
message: "两次密码输入不一致"
});
extend("required", {
...required,
message: "请输入{_field_}"
});
extend("eNameNum", value => {
let re = /^[a-zA-Z][\w\(\)\-]{6,32}$/;
let reg = /^[A-Za-z]+$/;
if (re.test(value) && !reg.test(value)) {
return true;
}
return "请输入正确的{_field_}(由6~32位大/小写字母,数字组合)";
});
extend("selected", {
...required,
message: "请选择{_field_}"
});
extend("checked", {
...required,
message: "请勾选{_field_}"
});
extend("alpha_num", {
...required,
message: "只能输入数字和字母"
});
extend("min", {
validate(value, args) {
return value.toString().length >= args.length;
},
params: ["length"],
message: "长度不能少于 {length} 个字符"
});
extend("max", {
validate(value, args) {
return value.toString().length <= args.length;
},
params: ["length"],
message: "长度不能超过 {length} 个字符"
});
extend("file", {
...required,
message: "请上传{_field_}"
});
extend("maxHasLabel", {
validate(value, args) {
return value.length <= args.length;
},
params: ["length"],
message: "{_field_}不能超过{length}个字符"
});
extend("section", {
validate(value, args) {
let temp = args.section.split("-");
if (value.length >= temp[0] && value.length <= temp[1]) {
return /^[0-9a-zA-Z]*$/g.test(value);
}
return false;
},
params: ["section"],
message: "请输入{section}位数字或字母"
});
extend("num", value => {
let re = /^[0-9]+$/;
if (re.test(value)) {
return true;
}
return "只能输入数字";
});
extend("nums", value => {
let re = /^\+?[1-9]\d*$/;
if (re.test(value)) {
return true;
}
return "只能输入大于0的正整数";
});
extend("numss", value => {
let re = /^\d+(\.\d{1,2})?$/;
if (re.test(value) && Number(value) > 0) {
return true;
}
return "只能输入大于0的数字,且保留两位小数";
});
extend("url", value => {
let re = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
if (re.test(value)) {
return true;
}
return "请输入正确的产品地址";
});
extend("mobile", value => {
let re = /^1[3456789]\d{9}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的手机号码";
});
extend("mobiles", value => {
let re = /^1[0123456789]\d{9}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的手机号码";
});
extend("menuCode", value => {
let re = /^MENU\d{6}$/;
if (re.test(value)) {
return true;
}
return "{_field_}格式错误";
});
extend("menuOrder", value => {
let re = /^[1-9]\d{0,7}$/;
if (re.test(value)) {
return true;
}
return "{_field_}格式错误";
});
extend("eNameNum", value => {
let re = /^[a-zA-Z][\w\(\)\-]{6,32}$/;
let reg = /^[A-Za-z]+$/;
if (re.test(value) && !reg.test(value)) {
return true;
}
return "请输入正确的{_field_}(由6~32位大/小写字母,数字组合)";
});
extend("httpUrl", value => {
let re = /^(http|https):\/\/([\w.]+\/?)\S/;
if (re.test(value)) {
return true;
}
return "{_field_}格式错误";
});
extend("looseUrl", value => {
let re = /^((http|https):\/\/)?([\w.]+\/?)\S/;
if (re.test(value)) {
return true;
}
return "{_field_}格式错误";
});
extend("checkPassword", value => {
var re = /^(?![A-Z]+$)(?![a-z]+$)(?!\d+$)(?![\W_]+$)\S{8,32}$/;
if (re.test(value)) {
return true;
}
return "请输入8-32个字符,至少包含大写字母、小写字母、数字和特殊字符中的两种";
});
extend("notChinese", value => {
let re = /^[^\u4e00-\u9fa5]+$/;
if (re.test(value)) {
return true;
}
return "不能包含中文";
});
extend("mixedInput", value => {
let re = /^[\u4e00-\u9fa5_a-zA-Z0-9-_.]+$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由中文字符、英文字母、数字、中划线、下划线和点组成)";
});
extend("pointStr", value => {
let re = /^[\u4e00-\u9fa5_a-zA-Z_·]+$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由字母、汉字和点组成)";
});
extend("decimalNum", (value, args) => {
let pointS = Number(args[0]) || 7;
let pointE = Number(args[1]) || 2;
let reg = new RegExp(
`^[+-]?(0|([1-9]\\d{0,${pointS}}))(\\.\\d{1,${pointE}})?$`,
"g"
);
let res = reg.test(value);
if (!res) {
return `最多输入小数点前${pointS + 1}位和小数点后${pointE}位`;
}
return true;
});
extend("decimalLeval", (value, args) => {
let min = Number(args[0]);
let max = Number(args[1]);
try {
let cur = Number(value);
if (cur >= min && cur <= max) {
return true;
}
return `只能输入${min}到${max}范围的值`;
} catch (e) {
return "请输入小数";
}
});
extend("decimalLeval2", (value, args) => {
let min = Number(args[0]);
let max = Number(args[1]);
let len = 0;
if (value.toString().indexOf(".") >= 0) {
len = value.split(".")[1].length;
}
try {
let cur = Number(value);
if (cur >= min && cur <= max && len <= 4) {
return true;
}
return `只能输入0.0001到99999999999999.9999范围的值`;
} catch (e) {
return "请输入小数";
}
});
extend("adCode", value => {
let re = /^PT\_[a-zA-Z0-9]{1,10}_[a-zA-Z0-9]{1,10}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(格式:PT_index_banner)";
});
extend("productNum", value => {
let re = /^ZB[a-zA-Z0-9]{1,100}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}";
});
extend("productName", value => {
let re = /^[\u4E00-\u9FA5A-Za-z0-9]+$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由数字、字母、汉字组成)";
});
extend("attrtNum", value => {
let re = /[^\u4e00-\u9fa5]{1,100}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(100以内,数字、字母、特殊字符组成)";
});
extend("strategyName", value => {
let re = /^[a-zA-Z][0-9a-zA-Z]{0,100}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由字母开头,数字、字母组成)";
});
extend("accessControl", value => {
let re = /^[a-zA-Z\u4e00-\u9fa5][\u4e00-\u9fa5a-zA-Z-z0-9]{0,49}$/;
if (re.test(value)) {
return true;
}
return "由中文、数字、字母组成,首字符不为数字";
});
extend("attrName", value => {
let re = /^[\u4e00-\u9fa5a-zA-Z-z0-9]+$/im;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由中文、数字、字母组成)";
});
extend("telephone", value => {
let re = /^[\(\)\s0-9-\+]{4,20}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的联系电话";
});
extend("selectRequired", {
...required,
message: "请选择{_field_}"
});
extend("productAreas", {
...required,
message: "请选择地区产品"
});
extend("selectMin", (value, args) => {
if (value.length > 0) {
return true;
}
return `请至少选择${args[0]}项`;
});
extend("discount", value => {
let re = /^\+?(?:\d(?:\.\d)?|10(?:\.0)?)$/;
if (re.test(value)) {
return true;
}
return "请输入0-10之内的数字,精确到小数点后1位";
});
extend("setDiscount", {
validate(value, bpdata) {
if (value == "0" || value > "1") {
return "请输入正确的折扣";
}
if (value == "" || value == null) {
return "请输入折扣";
}
if (bpdata[0] == "undefined" || bpdata[0] == "") {
bpdata[0] = 0;
}
if (value > 0 && value < bpdata[0]) {
return "客户云厂商折扣不能低于伙伴最低折扣";
}
let re = /^\d+(\.\d{1,2})?$/;
if (re.test(value)) {
return true;
}
if (!isNaN(value) && typeof value === "number") {
return true;
}
return "请输入正确的折扣";
}
});
extend("requiredData", {
validate(value, discount) {
if (discount[0] == "1") {
return;
}
}
});
extend("twoNum", value => {
let re = /^[1-9]$|^[1][0-9]$|^[2][0-8]$/;
if (re.test(value)) {
return true;
}
return "请输入1-28的数字";
});
extend("orderRule", value => {
let re = /^[0-9a-zA-Z,]*$/g;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}格式";
});
extend("nineNum", value => {
let re = /^\+?[1-9]{1}[0-9]{0,2}\d{0,0}$/;
if (re.test(value)) {
return true;
}
return "请输入1-999的整数";
});
extend("ninesNum", value => {
if (value.indexOf(".") != -1) {
let arr = value.split(".");
if (arr[1].length > 2) {
return "不能超过两位小数";
}
}
if (value <= 9999999.99 && value >= 0.01) {
return true;
} else {
return "请输入0.01-9999999.99的数字";
}
});
extend("after", {
validate(value, _a) {
var target = _a.target;
if (!!!value || !!!target) return true;
try {
if (new Date(value) < new Date(target)) {
return `{_field_}不能早于${_a.field}`;
} else {
return true;
}
} catch (error) {
return "请输入正确的时间格式";
}
},
params: [
{
name: "target",
isTarget: true
},
{
name: "field"
}
]
});
extend("before", {
validate(value, _a) {
var target = _a.target;
if (!!!value || !!!target) return true;
try {
if (new Date(value) > new Date(target)) {
return `{_field_}不能晚于${_a.field}`;
} else {
return true;
}
} catch (error) {
return "请输入正确的时间格式";
}
},
params: [
{
name: "target",
isTarget: true
},
{
name: "field"
}
]
});
extend("postattrName", value => {
let re = /^[\u4e00-\u9fa5a-zA-Z-z0-9(\)(\)[\]<\>_{\}]+$/im;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由中文、数字、字母组成)";
});
extend("recycleamount", {
validate(value, args = [0.01, 0.01]) {
let amount = args[0],
brokerAmount = args[1];
if (Number(value) > Number(amount)) {
return "拨款金额不能大于可拨款额度";
}
if (Number(value) > Number(brokerAmount)) {
return "拨款金额不能大于客户Broker余额";
} else {
return true;
}
}
});
extend("recoveryamount", {
validate(value, args = [0.01]) {
let amount = args[0];
if (Number(value) > Number(amount)) {
return "回收金额不能大于账户余额";
} else {
return true;
}
}
});
extend("maxNum", {
validate(value = 0, args = 0) {
if (!value || isNaN(value)) {
return "{_field_}格式不正确";
}
if (parseFloat(value) > parseFloat(args)) {
return `{_field_}不能超过${args}`;
}
return true;
}
});
extend("minNum", {
validate(value = 0, args = 0) {
if (!value || isNaN(value)) {
return "{_field_}格式不正确";
}
if (parseFloat(value) < parseFloat(args)) {
return `{_field_}不能低于${args}`;
}
return true;
}
});
extend("money", {
validate(value, args = [2, 0, 99999999.99]) {
let digit = args[0] || 2;
let minNum = args[1] || 0;
let maxNum = args[2] || 99999999.99;
if (isNaN(Number(value))) {
return `请输入${minNum}~${maxNum}的数字`;
}
let re = new RegExp(
`^(0|([1-9][0-9]*)|([0]\.[0-9]{1,2})|([1-9][0-9]*\.[0-9]{1,${digit}}))$`
);
if (digit == "0") {
re = new RegExp(`^[1-9][0-9]*$`);
}
if (re.test(value)) {
if (parseFloat(value) < minNum || parseFloat(value) > maxNum) {
return `请输入${minNum}~${maxNum}的数字`;
} else {
return true;
}
} else {
let valStr = value + "";
if (
valStr.indexOf(".") !== -1 &&
valStr.slice(valStr.indexOf(".") + 1).length > digit
) {
return `小数点后仅支持两位有效位数`;
}
return `请输入${minNum}~${maxNum}的数字`;
}
}
});
extend("dataName", value => {
let re = /^[\u4e00-\u9fa5_a-zA-Z0-9-_.\\(\\)\\【\\】\\(\\)\[.*\]]+$/;
if (re.test(value)) {
return true;
}
return "由中文字符、英文字母、数字、中划线、下划线和中英文小括号/中括号组成";
});
extend("mintomax", (value, args) => {
if (args[0] == "" && args[1]) {
return `请输入最小值`;
}
if (args[1] == "" && args[0]) {
return `请输入最大值`;
}
let min = Number(args[0]);
let max = Number(args[1]);
if (min >= max) {
return `最小值不能大于最大值`;
} else {
return true;
}
});
extend("IP", value => {
let re = /^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])(\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)){2}(\.(1\d{2}|2[0-4]\d|25[0-4]|[1-9]\d|[1-9]))$/;
if (re.test(value)) {
return true;
}
return "请输入正确的IP";
});
extend("dataName", value => {
let re = /^[\u4e00-\u9fa5_a-zA-Z0-9-_.\\(\\)\\【\\】\\(\\)\[.*\]]+$/;
if (re.test(value)) {
return true;
}
return "由中文字符、英文字母、数字、中划线、下划线和中英文小括号/中括号组成";
});
extend("phoneEmailRule", {
...required,
message: "手机号/邮箱至少填一项"
});
extend("alphaChinese", value => {
let re = /^[\u4e00-\u9fa5a-zA-Z-z]+$/im;
if (re.test(value)) {
return true;
}
return "由字母汉字组成";
});
extend("appCode", value => {
let re = /^(?!^\d+$)(?!^[a-zA-Z]+$)[0-9a-zA-Z]$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(英文字母开头,由4~32位大/小写字母,数字组合)";
});
extend("concurrency", {
validate(value, data) {
if (value) {
if (!data[0]) {
return "请选择单位";
}
}
if (value && data[0]) {
if (value > 1000) {
return "并发量不得大于1000";
}
}
return true;
}
});
extend("concurrency2", {
validate(value, data) {
console.log(value, data);
if (value) {
if (data == "null" || !data) {
return "请输入并发量";
}
}
return true;
}
});
extend("appQuota", value => {
if (value > 1000) {
return "配额不得大于1000";
}
return true;
});
extend("letters", value => {
let re = /^[a-zA-Z][a-zA-Z_]{1,31}$/;
if (re.test(value)) {
return true;
}
return "请输入英文字母";
});
extend("percentage", value => {
let re = /^(1|([1-9]\d{0,1})|100)$/;
if (re.test(value)) {
return true;
}
return "请输入1-100的整数";
});
extend("productNames", value => {
let re = /^[\u4E00-\u9FA5A-Za-z0-9+*]+$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由*、数字、字母、汉字组成)";
});
extend("apiNames", value => {
let re = /^[\u4E00-\u9FA50-9a-zA-Z]{1,49}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由汉字、数字、字母组成)";
});
extend("apiSerParamNames", value => {
let re = /^[a-zA-Z]{1,32}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由英文字母组成)";
});
extend("parameters", value => {
let re = /^[a-zA-Z][a-zA-Z_]{1,32}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由字母开头,英文和下划线组成)";
});
extend("validatePhones", value => {
let re = /^[a-zA-Z][a-zA-Z0-9]{0,50}$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由字母开头,英文和数字组成)";
});
extend("LettersandNumbers", value => {
let re = /^[A-Za-z0-9]+$/;
if (re.test(value)) {
return true;
}
return "请输入正确的{_field_}(由数字、字母组成)";
});
extend("letterNumConfine", {
validate(value, args) {
let re = /^[A-Za-z]+$/;
if (!re.test(value)) {
return `请输入正确的{_field_}(由字母组成)`;
}
if (value.length != args) {
return `只能输入${args}位字母`;
}
return true;
}
});
extend("LimitingUnderline", value => {
let re = /_/;
if (!re.test(value)) {
return true;
}
return "请输入正确的{_field_}(不包含下划线)";
});
extend("requiredss", {
...required,
message: "请输入异常内容"
});
extend("customName", value => {
let re = /^(?![-=+@])/;
if (re.test(value)) {
return true;
}
return "不能以-=+@开头";
});
extend("regex", value => {
let re = /^[\u4e00-\u9fa5a-zA-Z0-9]*([a-zA-Z][0-9][\u4e00-\u9fa5]|[\u4e00-\u9fa5a-zA-Z])[\u4e00-\u9fa5a-zA-Z0-9]*$/;
if (re.test(value)) {
return true;
}
return "不能输入纯数字、纯特殊字符,支持汉字、英文字母";
});
extend("backPaymentCode", value => {
let re = /^[A-z0-9\-\.]{0,64}$/;
if (re.test(value)) {
return true;
}
return "支持0-64位数字、字母、“-”“.”";
});
extend("percent", value => {
let re = /^(1|([1-9]\d{0,1})|100)$/;
if (re.test(value)) {
return true;
}
return "请输入1-100的整数";
});
extend("percents", value => {
let re = /^([0-9]|[1-2][0-9]|3[0-2])$/;
if (re.test(value)) {
return true;
}
return "请输入0-32的数字";
});
extend("chanceName", value => {
let re = /^[\u4e00-\u9fa5a-zA-Z0-9]{4,16}$/;
if (re.test(value)) {
return true;
}
return "{_field_}仅支持4-16位数字、字母、汉字";
});
extend("contractCode", value => {
let re = /^[a-zA-Z0-9\-]{1,127}$/;
if (re.test(value)) {
return true;
}
return "{_field_}仅支持数字、字母、短连接线,最大可输入127位";
});
extend("contractName", value => {
let re = /^[\u4e00-\u9fa5a-zA-Z0-9]{1,127}$/;
if (re.test(value)) {
return true;
}
return "{_field_}仅支持数字、字母、汉字,最大可输入127位";
});