当前位置:网站首页>格式校验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;
边栏推荐
猜你喜欢
【論文翻譯】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
Cube magique infini "simple"
深度学习分类网络 -- AlexNet
Spark概述
文件包含漏洞(二)
2022-2-14 learning xiangniuke project - section 23, section 5, development login and exit functions
Software testing Q & A
Matplotlib double Y axis + adjust legend position
Go language web development is very simple: use templates to separate views from logic
随机推荐
正则表达式总结
servlet的web.xml配置详解(3.0)
Practice C language advanced address book design
Common protocols and download paths of NR
Mathematical statistics and machine learning
With an amount of $50billion, amd completed the acquisition of Xilinx
Vscode paste image plugin saves image path settings
VSCode paste image插件保存图片路径设置
PHP read file (read JSON file, convert to array)
File contains vulnerability (I)
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
数理统计与机器学习
Technologists talk about open source: This is not just using love to generate electricity
LCD之MIPI协议的一些说明
Opencv LBP features
Zzuli:1061 sequential output of digits
Common websites for Postgraduates in data mining
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
软件测试 - 概念篇
线程池概述