Home Web Front-end JS Tutorial Various rules for javascript implementation inspection_javascript skills

Various rules for javascript implementation inspection_javascript skills

May 16, 2016 pm 03:47 PM
javascript test rule

The examples in this article describe various rules for JavaScript implementation verification. Share it with everyone for your reference. The details are as follows:

/**
 * 检验各种规则
 * @param str 检验的内容
 * @param cType 预设的检验规则 字符串[
 *       empty,  检验是否为空
 *       telphone, 座机手机号码
 *       allphone, 所有手机号码
 *       ydphone, 移动手机号码
 *       ltphone, 联通手机号码
 *       dxphone, 电信手机号码
 *       email,  邮箱
 *       url,  网址
 *       cn,   汉字
 *       image,  图片格式
 *       emscode, 邮政编码
 *       isEmpty, 检查是否为空
 *       isint,  整数
 *       isfloat, 判断是否为正小数
 *       isnumber, 判断为实数
 *       words,  判断是否为英文字母
 *       wordsAndNum,   判断是否为字母+数字
 *       wordsAndNumAndDownline, 判断是否由数字、26个英文字母或者下划线组成的字符串
 *       qq,      QQ检验
 *       personCard18,   身份证18位
 *       personCard15,   身份证15位
 *       ]
 * @param regex 自定义表达式 传入格式例如:"^\-?[1-9]+\d*$"
 *
 * @description cType 与 regex 只能有一个为空
 *    如 checkObjectByRegex("测试中文", "cn"); // 判断中文
 *    如 checkObjectByRegex("测试中文", null, "^[\u4e00-\u9fa5]+$"); // 自定义表达式正则
 * @return {boolean}
 */
function checkObjectByRegex(str, cType, regex) {
 /**
  * 定义验证各种格式类型的正则表达式对象
  */
 var Regexs = {
  telphone: (/^((\(\d{3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}$/), //座机手机号码
  allphone: (/^((13[0-9])|(14[57])|(15[0-9])|(17[678])|(18[0-9]))[0-9]{8}$/),   //所有手机号码
  ydphone: (/^((13[4-9])|(15[012789])|147|178|(18[23478]))[0-9]{8}$/),   //移动手机号码
  ltphone: (/^((13[0-2])|(145)|(15[56])|(176)|(18[56]))[0-9]{8}$/),     //联通手机号码
  dxphone: (/^((133)|(153)|(177)|(180)|(181)|(189))[0-9]{8}$/),    //电信手机号码
  email: (/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/),//邮箱
  url: (/(?:https|http|ftp|rtsp|mms):\/\/.+\/[\w]+\.[\w]+/),     //网址
  cn: (/^[\u4e00-\u9fa5]+$/i), //汉字
  image: (/\.jpg$|\.jpeg$|\.png$/i), //图片格式
  emscode: (/^[1-9]\d{5}$/), //邮政编码
  isint: (/^(\-)?[1-9]+\d*$/), //整数
  isfloat: (/^[0-9]+\.?[0-9]*$/), //判断是否为正小数
  isnumber: (/^[-\+]?\d+(\.\d+)?$/), //判断为实数
  words: (/^[A-Za-z]+$/), //判断是否为英文字母
  wordsAndNum: (/^[A-Za-z0-9]+$/), //判断是否为字母+数字
  wordsAndNumAndDownline: (/^\w+$/), //判断是否由数字、26个英文字母或者下划线组成的字符串
  qq: (/^[1-9]\d{4,11}$/), //QQ
  personCard18: (/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d|X)$/), //身份证18位
  personCard15: (/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/) //身份证15位
 };
 var nReg;
 if (str == null || typeof(str) == "undefined") {
  str = "";
 }
 if (cType != null && typeof(cType) != "undefined") {
  if (cType == "isEmpty") {
   str = $.trim(str);
   if (str != null && typeof(str) != "undefined" && str != "") {
    return false;
   } else return true;
  }
  nReg = Regexs[cType];
  if (str == null || str == "") return false; //输入为空,认为是验证通过
  // 针对 18位身份证单独处理
  if (cType == 'personCard18') {
   var ary = str.match(Regexs[cType]);
   if (!(parseInt(ary[3]) >= 1900)) return false;
   var D = new Date(ary[3] + "/" + ary[4] + "/" + ary[5]);
   var isTrue = D.getFullYear() == ary[3] && (D.getMonth() + 1) == ary[4] && D.getDate() == ary[5];
   return isTrue;
  }
  // 针对 15位身份证单独处理
  if (cType == 'personCard15') {
   var ary = str.match(Regexs[cType]);
   var D = new Date("19" + ary[3] + "/" + ary[4] + "/" + ary[5]);
   var isTrue = D.getYear() == ary[3] && (D.getMonth() + 1) == ary[4] && D.getDate() == ary[5];
   return isTrue;
  }
 } else {
  // 自定义正则表达式处理
  if (regex != null && typeof(regex) != "undefined") {
   nReg = new RegExp(regex);
  } else {
   return false;
  }
 }
 return nReg.test(str);
}
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

Rules and exceptions for pointer comparisons? Rules and exceptions for pointer comparisons? Jun 04, 2024 pm 06:01 PM

In C/C++, the pointer comparison rules are as follows: pointers pointing to the same object are equal. Pointers to different objects are not equal. Exception: Pointers to null addresses are equal.

See all articles