正则篇
1、邮箱
const isEmail = (v) => {
return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(v);
};
2、手机号码
const isMobile = (v, isStrict = false) => {
switch (isStrict) {
case false:
return /^1[0-9]{10}$/.test(v);
case true:
return /^1[3|4|5|6|7|8|9][0-9]{9}$/.test(v);
default:
return true;
}
};
3、座机号码
const isTel = (v, isStrict = false) => {
switch (isStrict) {
case false:
return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(v);
case true:
return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(v);
default:
return true;
}
};
4、url地址
const isURL = (v, isStrict = false) => {
switch (isStrict) {
case false:
return /^http[s]?:\/\/.*/.test(v);
case true:
return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(v);
default:
return true;
}
};
5、身份证
const isIdcard = (v) => {
return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(v);
};
6、邮政编码
const isPostal = (v) => {
return /[1-9]\d{5}(?!\d)/.test(v);
};
7、密码验证
const psw = (v) => {
//密码以字母开头,长度在6~18之间,只能包含字母、数字和下划线
return /^[a-zA-Z]\w{5,17}$/.test(v);
};
8、金额(小数点2位)
const money = (v) => {
return /^\d*(?:\.\d{0,2})?$/.test(v);
};
9、日期时间
const isDate = (v) => {
return (
/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(v)
|| /^(\d{4})\-(\d{2})\-(\d{2})$/.test(v)
);
};
10、数字
const isNum = (v) => {
return /^[0-9]$/.test(v);
};
11、英文
const isEnglish = (v) => {
return /^[a-zA-Z]+$/.test(v);
};
12、中文
const isChinese = (v) => {
return /^[\u4E00-\u9FA5]+$/.test(v);
};
13、小写
const isLower = (v) => {
return /^[a-z]+$/.test(v);
};
14、大写
const isUpper = (v) => {
return /^[A-Z]+$/.test(v);
};
15、HTML标签
const isHTML = (v) => {
return /<("[^"]*"|'[^']*'|[^'">])*>/.test(v);
};
类型判断篇
1、是否字符串
const isString = (v) => {
return (
typeof v == "string" &&
Object.prototype.toString.call(v).slice(8, -1) === "String"
);
};
2、是否数字
const isNumber = (v) => {
return (
typeof v === "number" &&
!isNaN(v) &&
Object.prototype.toString.call(v).slice(8, -1) === "Number"
);
};
3、是否boolean
const isBoolean = (v) => {
return (
typeof v === "boolean" &&
Object.prototype.toString.call(v).slice(8, -1) === "Boolean"
);
};
4、是否函数
const isFunction = (v) => {
return (
typeof v === "function" &&
Object.prototype.toString.call(v).slice(8, -1) === "Function"
);
};
5、是否null
const isNull = (v) => {
return Object.prototype.toString.call(v).slice(8, -1) === "Null";
};
6、是否undefined
const isUndefined = (v) => {
return (
typeof v === "undefined" &&
Object.prototype.toString.call(v).slice(8, -1) === "Undefined"
);
};
7、是否对象
const isObj = (v) => {
return Object.prototype.toString.call(v).slice(8, -1) === "Object";
};
8、是否数组
const isArray = (v) => {
return (
Array.isArray(v) &&
Object.prototype.toString.call(v).slice(8, -1) === "Array"
);
};
9、是否时间
const isDate = (v) => {
return (
v instanceof Date &&
Object.prototype.toString.call(v).slice(8, -1) === "Date"
);
};
10、是否正则
const isRegExp = (v) => {
return Object.prototype.toString.call(v).slice(8, -1) === "RegExp";
};
11、是否错误对象
const isError = (v) => {
return Object.prototype.toString.call(v).slice(8, -1) === "Error";
};
12、是否Symbol函数
const isSymbol = (v) => {
return Object.prototype.toString.call(v).slice(8, -1) === "Symbol";
};
13、是否Promise对象
const isPromise = (v) => {
return Object.prototype.toString.call(v).slice(8, -1) === "Promise";
};
14、是否Set对象
const isSet = (v) => {
return Object.prototype.toString.call(v).slice(8, -1) === "Set";
};
===============未完待续================