工具库 --- Validator (JS正则)
工具库 --- Validator
今天写的是一个正则验证类
单例模式
工具库地址:github.com/WeForStudy/…
npm地址:www.npmjs.com/package/slm…
单例模式
减少不必要的对象生存,减少资源的占用
由于只需要new一次,项目中其他项目共用一个对象
静态属性 instance --> private
静态方法getInstance --> public
其他方法,静态
源码
包含(邮箱、手机、数字、http地址、纯英文、包含汉字等验证)
export class ValidatorUtil {private static instance = null;/*** @description private the constructor in case other new it*/private constructor() {}/*** @description get the instance of Validator*/public static getInstance(): ValidatorUtil {if (!this.instance) {this.instance = new ValidatorUtil();}return this.instance;}/*** @description inside method will be use in every method* @param {String} str the string will be checked later* @returns {Boolean} the result*/private regTest(reg: RegExp, str: any): boolean {return reg.test(str);}/*** @description check string whether is a url or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private formatUrl(str: string): boolean {const reg = /^(https|http):\/\//g;return this.regTest(reg, str);}/*** @description check string whether is a email url or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private formatEmail(str: string): boolean {const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/g;return this.regTest(reg, str);}/*** @description check string whether is a url or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private formatCnPhone(str: string): boolean {const reg = /^1(3|4|5|6|7|8|9)\d{9}$/g;return this.regTest(reg, str);}/*** @description check string whether is only a en char or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private onlyIncludeEn(str: string): boolean {const reg = /[^a-zA-Z]/g;return this.regTest(reg, str);}/*** @description check string whether is Number or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private includeNumber(str: any): boolean {return this.regTest(/[0-9]/g, str);}/*** @description check string whether is only include number or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private onlyNumber(str: any): boolean {return !this.regTest(/[^0-9]/g, str);}/*** @description check string whether include blank or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private includeBlank(str: any): boolean {return this.regTest(/\s+|\s+$/g, str);}/*** @description check string whether is special char or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private includeSpecialChar(str: string): boolean {const regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im,regCn = /[·!#¥(——):;“”‘、,|《。》?、【】[\]]/im;if (this.includeBlank(str)) return true;if (this.regTest(regEn, str)) return true;return this.regTest(regCn, str);}/*** @description check string whether include blank or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private includeEnlishAndChineseChar(str: any): boolean {return this.regTest(/^[\u4e00-\u9fa5a-z]+$/gi, str);}/*** @description check string whether include special char or not* @param {String} str the string will be checked later* @returns {Boolean} the result*/private includeSpecCharAndExcludeSimpleChar(str: string): boolean {const regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/im,regCn = /[·!#¥(——)|《》【】[\]]/im; // exclude , 、“”‘’ 。 ?;:if (this.regTest(regEn, str)) return true;return this.regTest(regCn, str);}}复制代码
使用案例
const slmUtils = require('../build'); const assert = require('assert');const { Validator } = slmUtils; describe('Util test', () => {describe('Validator check', () => {it('should be true when value include number (includeNumber)', () => {const objA = '123abc';assert.equal(Validator.getInstance().includeNumber(objA), true);});it('should be false when value not only include number (onlyNumber)', () => {const objA = '123abc';assert.equal(Validator.getInstance().onlyNumber(objA), false);});it('should be true when values format include http:// or https:// (formatUrl)', () => {const objA = 'https://';assert.equal(Validator.getInstance().formatUrl(objA), true);});it('should be true when values format normal Phone', () => {const objA = '13212312321';assert.equal(Validator.getInstance().formatCnPhone(objA), true);});it('should be true when values format normal Emial address', () => {const objA = 'trest@xxx.com';assert.equal(Validator.getInstance().formatEmail(objA), true);});it('should be false when values not only include EN Char', () => {const objA = 'asd12';assert.equal(Validator.getInstance().formatCnPhone(objA), false);});it('should be true when values include Blank', () => {const objA = 'asd 12';assert.equal(Validator.getInstance().includeBlank(objA), true);});it('should be true when values include Special char', () => {const objA = 'asd 12';assert.equal(Validator.getInstance().includeSpecialChar(objA), true);});it('should be true when values include Chinese char', () => {const objA = '你';assert.equal(Validator.getInstance().includeEnlishAndChineseChar(objA), true);});it('should be false when values include , char', () => {const objA = 'ab,d';assert.equal(Validator.getInstance().includeSpecCharAndExcludeSimpleChar(objA), true);});}); }); 复制代码
转载于:https://juejin.im/post/5c949a6f6fb9a0710a1bcb7e
工具库 --- Validator (JS正则)相关推荐
- 论文研读-图可视化-NetV.js:Web端可视化工具库
NetV.js:一个基于网络的用于大规模图和网络的高效可视化的库 1 论文概述 1.1 文章摘要 1.2 引言 1.3 文章脉络 2 相关工作 3 设计 3.1 设计要求 3.2 设计细节 3.2.1 ...
- JavaScript进阶学习(二)—— 基于原型链继承的js工具库的实现方法
文章来源:小青年原创 发布时间:2016-07-03 关键词:JavaScript,原型链,jQuery类库 转载需标注本文原始地址: http://zhaomenghuan.github.io... ...
- GitBook是一个命令行工具(Node.js库),我们可以借用该工具使用Github/Git和Markdown来制作精美的图书,但它并不是一本关于Git的教程哟。...
GitBook是一个命令行工具(Node.js库),我们可以借用该工具使用Github/Git和Markdown来制作精美的图书,但它并不是一本关于Git的教程哟. 支持输出多种格式 GitBook支 ...
- JS工具库moment —— 实现日历
moment moment是一个js工具库,这个库中封装的是日期时间的方法,功能很全面.可以去moment官网看看,它的中文文档介绍的也很详细,主要是看一下方法的使用.附上官网地址:moment.js ...
- 10个常用的JS工具库
10个常用的JS工具库,80%的项目都在用! Avue Cloud 2022-01-26 09:46 图片 高手区别于普通人的重要一点是,他们善于利用工具,把更多的时间留给了规划和思考.写代码也是同样 ...
- IT:前端进阶技术路线图(初级→中级→高级)之初级(研发工具/HTML/CSS/JS/浏览器)/中级(研发链路/工程化/库/框架/性能优化/工作原理)/高级(搭建/中后台/体验管理等)之详细攻略
IT:前端进阶技术路线图(初级→中级→高级)之初级(研发工具/HTML/CSS/JS/浏览器)/中级(研发链路/工程化/库/框架/性能优化/工作原理)/高级(搭建/Node/IDE/中后台/体验管理/ ...
- 当前最流行的 js 工具库
高手区别于普通人的重要一点是,他们善于利用工具,把更多的时间留给了规划和思考.写代码也是同样的道理,工具用好了,你就有更多的时间来规划架构和攻克难点.今天就给大家分享一下当前最流行的 js 工具库,如 ...
- Lodash.js:实用的工具库
Lodash.js:实用的工具库 如官方所介绍的那样,Lodash是一个具有一致接口.模块化.高性能的JavaScript工具库.一开始Lodash只是Underscore.js的一个fork,之后再 ...
- Driver.js - 开源无依赖的 web 新手交互引导工具库,功能强大、高度可定制
可以快速实现新手引导效果,自带动画,体验优秀,而且还能高度定制. 关于 Driver.js Driver.js 是一个可以轻松实现新手指引交互的 javascript 工具库,主要的作用是为刚接触应用 ...
最新文章
- 可编辑选择、删除条目的ListView
- python声音信号调制_用python产生正弦波和PWM信号产生脉冲幅度调制
- 线框模型_进行计划之前:线框和模型
- Linux_2.6字符设备驱动实例
- sip协议详解_SIP协议详解-INVITE消息发送过程
- vue 毫秒数转年月日_Vue将毫秒数转化为正常日期格式的实例_盂希_前端开发者
- 计算机无法安装应用,编程软件无法安装或报错
- 不能执行已释放 Script 的代码
- Access安全性之QA详解
- Windows PE 背景知识
- [转]瀑布流布局浅析
- [转自华尔街的强帖]怎样才能嫁给有钱人
- 如何系统学习SWAT模型—建模方法、实例应用、高级进阶
- html两个部分组成部分组成,html页面由哪几部分组成
- 在MySQL数据库中进行模糊查询_如何实现mysql数据库单表的模糊查询?
- SPDK/NVMe存储技术分析之用户态ibv_post_send()源码分析(一)
- 红帽linux7.2安装教程,RHEL 6.2安装(超级详细图解教程) | 系统运维
- SEO优化应该照用户的需求去做
- 新课程盘古人工智能框架开发专题发布,智华欢迎读者学习!
- js一行If ... else ... else if语句
热门文章
- VC解析XML--使用CMarkup类解析XML
- IntellJ_打开选中的文件所在的文件夹
- 《数据科学R语言实践:面向计算推理与问题求解的案例研究法》一一2.1 引言...
- antd+dva笔记
- 一次图文并茂的***完整测试二
- 一个用微软官方的OpenXml读写Excel 目前网上不太普及的方法。
- WIN7 任务栏放右侧 有个BUG
- iOS经典讲解之获取沙盒文件路径写入和读取简单对象
- Node.js入门(含NVM、NPM、NVM的安装)
- 异步备份和还原数据库:.NET发现之旅(六)