当前位置:网站首页>格式校验js
格式校验js
2022-07-02 05:54:00 【Serena_tz】
let util = {};
// 将json格式转成echart data格式
util.jsonToList = function jsonToList(json) {
if (json) {
return Object.keys(json).map(key => {
return {
name: key,
value: json[key]
};
});
}
};
// 手机号格式校验
util.validatephone = function (rule, value, callback, mag, msg) {
if (!value) {
return callback(new Error('手机号不可为空'));
} else if (!/^1[1|2|3|4|5|6|7|8|9][0-9]\d{8}$/.test(value)) {
return callback(new Error('手机号输入错误'));
} else {
callback();
}
};
// 邮箱格式校验
util.validatemail = function (rule, value, callback, mag, msg) {
if (!value) {
return callback(new Error('邮箱不可为空'));
} else if (!(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/.test(value))) {
return callback(new Error('邮箱输入错误'));
} else {
callback();
}
};
util.singleValidIP = function (str) {
// IP地址合法校验: 排除 0.0.0.0; 排除 255.255.255.255; 支持区间录入
let reg = /^((25[0-5]|2[0-4]\d|[1]{1}\d{1}\d{1}|[1-9]{1}\d{1}|\d{1})($|(?!\.$)\.)){4}$/;
let regS = /^((25[0-5]|2[0-4]\d|[1]{1}\d{1}\d{1}|[1-9]{1}\d{1}|\d{1})($|(?!\.$)\.))$/;
let regZ = /(^0{1,3}(\.0{1,3}){3}$)/;
let regM = /(^255(\.255){3}$)/;
if (/([-])/.test(str)) {
// 支持区间录入
let arr = str.split('-');
if (arr.length === 2) {
return reg.test(arr[0]) && regS.test(arr[1]);
} else {
return false;
}
} else {
return reg.test(str) && !regZ.test(str) && !regM.test(str);
}
};
util.getSessionStorage = function (key) {
if (sessionStorage.getItem(key)) {
let data = sessionStorage.getItem(key);
try {
return JSON.parse(data);
} catch (e) {
return data;
}
}
return '';
}
util.setSessionStorage = function (key, data) {
if (typeof data === 'undefined') {
data = '';
}
data = JSON.stringify(data);
sessionStorage.setItem(key, data);
}
util.removeStorageData = function (key) {
sessionStorage.removeItem(key);
}
// //获取url中参数
util.getUrlParam = function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURI(r[2]);
return null;
}
export default util;
边栏推荐
猜你喜欢

文件包含漏洞(二)

all3dp.com网站中全部Arduino项目(2022.7.1)
![[personal test] copy and paste code between VirtualBox virtual machine and local](/img/ce/eaf0bd9eff6551d450964da72e0b63.jpg)
[personal test] copy and paste code between VirtualBox virtual machine and local

Matplotlib double Y axis + adjust legend position

数理统计与机器学习

Lantern Festival gift - plant vs zombie game (realized by Matlab)

2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions

Practice C language advanced address book design
![[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP](/img/25/73f11ab2711ed2cc9f20bc7f9116b6.png)
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP

Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
随机推荐
Vite打包后的dist不能直接在浏览器打开吗
[Chongqing Guangdong education] selected reading reference materials of British and American literature of Nanyang Normal University
数理统计与机器学习
servlet的web.xml配置详解(3.0)
TI毫米波雷达学习(一)
1035 Password
软件测试基础篇
token过期自动续费方案和实现
“简单”的无限魔方
Uva548 tree
Generics and generic constraints of typescript
js判断移动端还是pc端
Thread pool overview
Addchild() and addattribute() functions in PHP
PHP parent
php继承(extends)
Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
How to change the IP address of computer mobile phone simulator
Oled12864 LCD screen
【LeetCode】Day92-盛最多水的容器