当前位置:网站首页>格式校验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;
边栏推荐
- VSCode paste image插件保存图片路径设置
- php内的addChild()、addAttribute()函数
- 数据库学习总结5
- 脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四
- CNN可视化技术 -- CAM & Grad-CAM详解及pytorch简洁实现
- Software testing learning - day 4
- Lingyunguang rushes to the scientific innovation board: the annual accounts receivable reaches 800million. Dachen and Xiaomi are shareholders
- PHP extensions
- [golang syntax] be careful with the copy of slices
- Alibaba: open source and self-developed liquid cooling data center technology
猜你喜欢
随机推荐
"Simple" infinite magic cube
15 C language advanced dynamic memory management
【LeetCode】Day92-盛最多水的容器
Unity Shader 学习笔记(3)URP渲染管线带阴影PBR-Shader模板(ASE优化版本)
Eco express micro engine system has supported one click deployment to cloud hosting
Spark概述
TI毫米波雷达学习(一)
【论文翻译】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
PHP extensions
How to change the IP address of computer mobile phone simulator
Cube magique infini "simple"
c语言中的几个关键字
使用sha256文件验证下载的文件
Some descriptions of Mipi protocol of LCD
数据挖掘方向研究生常用网站
Here comes a new chapter in the series of data conversion when exporting with easyexcel!
500. 键盘行
1035 Password
Generics and generic constraints of typescript
php获取cpu使用率、硬盘使用、内存使用