正则测试地址:http://tool.oschina.net/regex/#
正则表达式收集网
正则表达式详细规则
const objRegExp = /^[0-9]\d*(.\d+)?$/;
objRegExp.test(strValue);
1. 小数:/^[0-9]\d*(\.\d+)?$/
2. 整数: /^[+]{0,1}(\d+)$/ --->优化后 /^[+]?(\d+)$/
3. 大于0包含小数整数:/^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/ --->优化后 /^(?!(0[0-9]*$))[0-9]+[.]*[0-9]*$/
4. 0-100的小数 : /^(100|(([1-9]\d|\d)(\.\d{1,2})?))$/
5. 0-100 百分比: /^-?(100|(([1-9]\d|\d)(\.\d{1,2})?))%$/ (可以负数) /^(100|(([1-9]\d|\d)(\.\d{1,2})?))%$/
6. 0-100的数(整数/小数): /^((\d{1,2}(\.\d{1,2})?)|100|100.00)$/
7. 1-100整数: /^([1-9]|[1-9]\d|100)$/
8. 0-100整数: /^(\d|[1-9]\d|100)$/
9. 年月正则:/^\d{4}-((0([1-9]))|(1(0|1|2)))$/
10. 验证分数正则(0-100):/^(100|(([1-9]\d|\d)(\.(0|5){1})?))$/
11.匹配金额(保留2位小数):/(^[1-9]\d*(\.\d{1,2})?$)|(^0(\.\d{1,2})?$)/
12.正则端口号(1-65535):/^([1-9](\d{0,3}))$|^([1-5]\d{4})$|^(6[0-4]\d{3})$|^(65[0-4]\d{2})$|^(655[0-2]\d)$|^(6553[0-5])$/
ip地址、子网掩码参考地址
ip地址:/^(\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])$/
子网掩码校验:/^(254|252|248|240|224|192|128|0)\.0\.0\.0|255\.(254|252|248|240|224|192|128|0)\.0\.0|255\.255\.(254|252|248|240|224|192|128|0)\.0|255\.255\.255\.(254|252|248|240|224|192|128|0)$/
常用检验方法
//检测用户身份证号
function checkCentNo(id) {
// 1 "验证通过!", 0 //校验不通过
var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
var format = /^(([1][1-5])|([2][1-3])|([3][1-7])|([4][1-6])|([5][0-4])|([6][1-5])|([7][1])|([8][1-2]))\d{4}(([1][9]\d{2})|([2]\d{3}))(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))\d{3}[0-9xX]$/;
//号码规则校验
if (!format.test(id)) {
return { 'status': 0, 'msg': '身份证城市编码不合规' };
}
if(id.startsWith("54") || id.startsWith("65") || id.startsWith("5117")){
return { 'status': 0, 'msg': '身份证城市编码不发货' };
}
//区位码校验
//出生年月日校验 前正则限制起始年份为1900;
var year = id.substr(6, 4),//身份证年
month = id.substr(10, 2),//身份证月
date = id.substr(12, 2),//身份证日
time = Date.parse(month + '-' + date + '-' + year),//身份证日期时间戳date
now_time = Date.parse(new Date()),//当前时间戳
dates = (new Date(year, month, 0)).getDate();//身份证当月天数
if (time > now_time || date > dates) {
return { 'status': 0, 'msg': '身份证出生日期不合规' }
}
//校验码判断
var c = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); //系数
var b = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); //校验码对照表
var id_array = id.split("");
var sum = 0;
for (var k = 0; k < 17; k++) {
sum += parseInt(id_array[k]) * parseInt(c[k]);
}
if (id_array[17].toUpperCase() != b[sum % 11].toUpperCase()) {
return { 'status': 0, 'msg': '身份证校验码不合规' }
}
//获取年龄
var myDate = new Date();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
var age = myDate.getFullYear()-id.substring(6, 10) - 1;
if (id.substring(10,12)<month||id.substring(10,12)==month&&id.substring(12,14)<=day)
{
age++;
}
if(age<16){
return { 'status': 0, 'msg': '年龄不能小于16周岁!' }
}
return { 'status': 1, 'msg': '校验通过' }
}
//检测汉字长度
function chineseLen(txt) {
var n = 0;
var len = txt.length;
for (var i = 0; i < len; i += 1) {
if (/[\u4E00-\u9FA5\u3400-\u4DB5\u9FA6-\u9FBB\uF900-\uFA2D\uFA30-\uFA6A\uFA70-\uFAD9]/.test(txt.charAt(i))) {
n += 1;
}
}
return n;
}
// 校验非法字符
function checkScript (text) {
var flag = false;
var scriptWord = "<|>|script|alert|{|}|#|$|'|\"|:|;|&|*|@|@|%|^|?";
var words = scriptWord.split('|');
for (var i = 0; i < words.length; i += 1) {
if (text.indexOf(words[i]) !== -1) {
flag = true;
break;
}
}
return flag;
};