格式校验
howcode 2022-12-30 0 javascript
# 校验身份证号码
export const checkCardNo = (value) => {
let reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
return reg.test(value);
};
1
2
3
4
2
3
4
# 校验是否包含中文
export const haveCNChars => (value) => {
return /[\u4e00-\u9fa5]/.test(value);
}
1
2
3
2
3
# 校验是否为中国大陆的邮政编码
export const isPostCode = (value) => {
return /^[1-9][0-9]{5}$/.test(value.toString());
}
1
2
3
2
3
# 校验是否为IPv6地址
export const isIPv6 = (str) => {
return Boolean(str.match(/:/g)?str.match(/:/g).length<=7:false && /::/.test(str)?/^([\da-f]{1,4}(:|::)){1,6}[\da-f]{1,4}$/i.test(str):/^([\da-f]{1,4}:){7}[\da-f]{1,4}$/i.test(str));
}
1
2
3
2
3
# 校验是否为邮箱地址
export const isEmail = (value) {
return /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value);
}
1
2
3
2
3
# 校验是否为中国大陆手机号
export const isTel = (value) => {
return /^1[3,4,5,6,7,8,9][0-9]{9}$/.test(value.toString());
}
1
2
3
2
3
# 校验是否包含emoji表情
export const isEmojiCharacter = (value) => {
value = String(value);
for (let i = 0; i < value.length; i++) {
const hs = value.charCodeAt(i);
if (0xd800 <= hs && hs <= 0xdbff) {
if (value.length > 1) {
const ls = value.charCodeAt(i + 1);
const uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
return true;
}
}
} else if (value.length > 1) {
const ls = value.charCodeAt(i + 1);
if (ls == 0x20e3) {
return true;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff) {
return true;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
return true;
} else if (0x2934 <= hs && hs <= 0x2935) {
return true;
} else if (0x3297 <= hs && hs <= 0x3299) {
return true;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030
|| hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b
|| hs == 0x2b50) {
return true;
}
}
}
return false;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 判断今天是否为本月最后一天
export const isLastDayOfMonth = () =>{
var flag = false;
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
var today = 31;
var new_year = year; //取当前的年份
var new_month = month++; //取下一个月的第一天,方便计算(最后一天不固定)
if (month > 12) { //如果当前大于12月,则年份转到下一年
new_month -= 12; //月份减
new_year++; //年份增
}
var new_date = new Date(new_year, new_month, 1); //取当年当月中的第一天
var month_last_day = (new Date(new_date.getTime() - 1000 * 60 * 60 * 24)).getDate();
if (today == month_last_day) {
flag = true;
}
return flag;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 车牌号的校验
export const isCarNo(carNo)=>{
const carNoReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
return carNoReg.test(carNo);
}
1
2
3
4
2
3
4
# 整数的校验
export const isInt (number)=>{
const intReg = /^[-+]?\d*$/;
return intReg.test(number);
}
1
2
3
4
2
3
4
# 小数的校验
export const isFloat (number)=>{
const floatReg = /^[-\+]?\d+(\.\d+)?$/;
return floatReg.test(number);
}
1
2
3
4
2
3
4
# 文件拓展名的校验
export const checkFileName (arr,filename)=>{
arr = arr.map(name => `.${name}`).join('|');
const filenameReg = new RegExp(`(${arr})$`);
return filenameReg.test(filename);
}
1
2
3
4
5
2
3
4
5
评论
- 表情
——暂无评论——