Summary of JS Common Simple Regular Expression Verification Function [Mobile Phone Address Enterprise Tax Number Amount ID Card etc.]

  • 2021-07-13 04:14:21
  • OfStack

This paper summarizes the common simple regular expression validation functions of JS with examples. Share it for your reference, as follows:

The following are some common and simple validations, such as those special and complicated situations, which are not considered here

1. Verify the phone number or mobile phone number


/**
 *  Verify the phone number (mobile phone number + Telephone number) 
 * @param obj
 * @returns {Boolean}
 */
function checkPhoneNum(obj){
  if(/^((\d{3}-\d{8}|\d{4}-\d{7,8})|(1[3|5|7|8][0-9]{9}))$/.test(obj)){
    return true;
  }
}

2. Verify the address (Chinese, English and numbers)


/**
 *  Verify business address (Chinese, English, numbers) 
 * @param obj
 */
function checkAddress(obj){
  if(/^[\u4e00-\u9fa5a-zA-Z0-9]+$/.test(obj)){
    return true;
  }
}

3. Verify the user name


/**
 *  Verify the user name input format 
 * @param obj
 * @returns {Boolean}
 */
function checkUserName(obj){
  if(/^[a-zA-Z0-9_-]{3,16}$/.test(obj)){
    return true;
  }
}

4. Verify the password


/**
 *  Verify password input format 
 * @param obj
 * @returns {Boolean}
 */
function verifyPassword(obj){
  if(/^[a-z0-9_-]{5,18}$/.test(obj)){
    return true;
  }
}

5. Verify the enterprise tax number


/**
 *  Verify tax number 
 * 15 Or 17 Or 18 Or 20 Composed of letters and numbers 
 * @param obj
 * @returns {Boolean}
 */
function checkTax(obj){
  if(/^[A-Z0-9]{15}$|^[A-Z0-9]{17}$|^[A-Z0-9]{18}$|^[A-Z0-9]{20}$/.test(obj)){
    return true;
  }
}

6. Verify the amount


/**
 *  Verified amount (verifiable   Greater than or equal to zero, less than or equal to zero 99999999.99  Number of) 
 * @param obj
 * @returns {Boolean}
 */
function checkMoney(obj){
  if(/^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/.test(obj)){
    return true;
  }
}

7. Verify the ID number, including the mainland ID number, and the Hong Kong, Macao and Taiwan ID number. Of course, this is just a simple specification 1. The actual check bits in the last brackets like the Hong Kong ID number need to be based on the previous

The number is calculated according to the weighting algorithm determined by 1 for verification, which is not involved here. If you need special accuracy, you can study it once.


/**
 *  Verify the ID number of the mainland 
 *  Verify ID number 
 *  ID card number is 15 Bit or 18 The full number of bits, or 18 Bit time preceding 17 Bits are numbers, and last 1 Bits are parity bits, which may be numbers or characters X Or x
 * @param obj
 */
function checkIdCard(obj){
  if(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(obj)){
    return true;
  }
}
/**
 *  Verify Hong Kong, Macao and Taiwan ID cards 
 * @param obj
 */
function checkGATIdCard(obj){
  var reg1 = /^[A-Z]{1,2}[0-9]{6}[\(|\ ( ]?[0-9A-Z][\)|\ ) ]?$/;// Hong Kong format 1 ( Hong Kong identity card number structure: XYabcdef(z))
  var reg2 = /^[A-Z][0-9]{8,12}$/;// Hong Kong format 2 (H60152555)
  var reg3 = /^[1|5|7][0-9]{6}[\(|\ ( ]?[0-9A-Z][\)|\ ) ]?$/;// Macao ,8 Number of digits , Excluding date of birth   Format is  xxxxxxx(x)  Note :x All numbers , No English letters   The first digit is only 1 , 5 , 7 Beginning with the word 
  var reg4 = /^[a-zA-Z][0-9]{9}$/;// Taiwan :10 Bit letters and numbers 
  if(reg1.test(obj) || reg2.test(obj) || reg3.test(obj) || reg4.test(obj)){
    return true;
  }
}

The following is the authoritative writing of the verification ID number:


/**
 * 身份证15位编码规则:dddddd yymmdd xx p
 * dddddd:地区码
 * yymmdd: 出生年月日
 * xx: 顺序类编码,无法确定
 * p: 性别,奇数为男,偶数为女
 * 身份证18位编码规则:dddddd yyyymmdd xxx y
 * dddddd:地区码
 * yyyymmdd: 出生年月日
 * xxx:顺序类编码,无法确定,奇数为男,偶数为女
 * y: 校验码,该位数值可通过前17位计算获得
 * 18位号码加权因子为(从右到左) Wi = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1 ]
 * 验证位 Y = [ 1, 0, X, 9, 8, 7, 6, 5, 4, 3, 2 ]
 * 校验位计算公式:Y_P = mod( ∑(Ai×Wi),11 )
 * i为身份证号码从右往左数的 2...18 位; Y_P为脚丫校验码所在校验码数组位置
 *
 */
// 构造函数,变量为15位或者18位的身份证号码
function IdCardValidate(CardNo) {
 this.Valid = false;
 this.ID15 = '';
 this.ID18 = '';
 this.Local = '';
 if (CardNo != null)
  this.SetCardNo(CardNo);
}
// 设置身份证号码,15位或者18位
IdCardValidate.prototype.SetCardNo = function(CardNo) {
 this.ID15 = '';
 this.ID18 = '';
 this.Local = '';
 CardNo = CardNo.replace(" ", "");
 var strCardNo;
 if (CardNo.length == 18) {
  pattern = /^\d{17}(\d|x|X)$/;
  if (pattern.exec(CardNo) == null)
   return;
  strCardNo = CardNo.toUpperCase();
 } else {
  pattern = /^\d{15}$/;
  if (pattern.exec(CardNo) == null)
   return;
  strCardNo = CardNo.substr(0, 6) + '19' + CardNo.substr(6, 9)
  strCardNo += this.GetVCode(strCardNo);
 }
 this.Valid = this.CheckValid(strCardNo);
}
// 校验身份证有效性
IdCardValidate.prototype.IsValid = function() {
 return this.Valid;
}
// 返回生日字符串,格式如下,1981-10-10
IdCardValidate.prototype.GetBirthDate = function() {
 var BirthDate = '';
 if (this.Valid)
  BirthDate = this.GetBirthYear() + '-' + this.GetBirthMonth() + '-'
    + this.GetBirthDay();
 return BirthDate;
}
// 返回生日中的年,格式如下,1981
IdCardValidate.prototype.GetBirthYear = function() {
 var BirthYear = '';
 if (this.Valid)
  BirthYear = this.ID18.substr(6, 4);
 return BirthYear;
}
// 返回生日中的月,格式如下,10
IdCardValidate.prototype.GetBirthMonth = function() {
 var BirthMonth = '';
 if (this.Valid)
  BirthMonth = this.ID18.substr(10, 2);
 if (BirthMonth.charAt(0) == '0')
  BirthMonth = BirthMonth.charAt(1);
 return BirthMonth;
}
// 返回生日中的日,格式如下,10
IdCardValidate.prototype.GetBirthDay = function() {
 var BirthDay = '';
 if (this.Valid)
  BirthDay = this.ID18.substr(12, 2);
 return BirthDay;
}
// 返回性别,1:男,0:女
IdCardValidate.prototype.GetSex = function() {
 var Sex = '';
 if (this.Valid)
  Sex = this.ID18.charAt(16) % 2;
 return Sex;
}
// 返回15位身份证号码
IdCardValidate.prototype.Get15 = function() {
 var ID15 = '';
 if (this.Valid)
  ID15 = this.ID15;
 return ID15;
}
// 返回18位身份证号码
IdCardValidate.prototype.Get18 = function() {
 var ID18 = '';
 if (this.Valid)
  ID18 = this.ID18;
 return ID18;
}
// 返回所在省,例如:上海市、浙江省
IdCardValidate.prototype.GetLocal = function() {
 var Local = '';
 if (this.Valid)
  Local = this.Local;
 return Local;
}
//获取身份证号码为18位时最后的验证位
IdCardValidate.prototype.GetVCode = function(CardNo17) {
 var Wi = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
 var Ai = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
 var cardNoSum = 0;
 for (var i = 0; i < CardNo17.length; i++)
  cardNoSum += CardNo17.charAt(i) * Wi[i];
 var seq = cardNoSum % 11;
 return Ai[seq];
}
IdCardValidate.prototype.CheckValid = function(CardNo18) {
 if (this.GetVCode(CardNo18.substr(0, 17)) != CardNo18.charAt(17))
  return false;
 if (!this.IsDate(CardNo18.substr(6, 8)))
  return false;
 var aCity = {
  11 : "北京",
  12 : "天津",
  13 : "河北",
  14 : "山西",
  15 : "内蒙古",
  21 : "辽宁",
  22 : "吉林",
  23 : "黑龙江 ",
  31 : "上海",
  32 : "江苏",
  33 : "浙江",
  34 : "安徽",
  35 : "福建",
  36 : "江西",
  37 : "山东",
  41 : "河南",
  42 : "湖北 ",
  43 : "湖南",
  44 : "广东",
  45 : "广西",
  46 : "海南",
  50 : "重庆",
  51 : "4川",
  52 : "贵州",
  53 : "云南",
  54 : "西藏 ",
  61 : "陕西",
  62 : "甘肃",
  63 : "青海",
  64 : "宁夏",
  65 : "新疆",
  71 : "台湾",
  81 : "香港",
  82 : "澳门",
  91 : "国外"
 };
 if (aCity[parseInt(CardNo18.substr(0, 2))] == null)
  return false;
 this.ID18 = CardNo18;
 this.ID15 = CardNo18.substr(0, 6) + CardNo18.substr(8, 9);
 this.Local = aCity[parseInt(CardNo18.substr(0, 2))];
 return true;
}
//验证18位数身份证号码中的生日是否是有效生日
IdCardValidate.prototype.IsDate = function(strDate) {
 var r = strDate.match(/^(\d{1,4})(\d{1,2})(\d{1,2})$/);
 if (r == null)
  return false;
 var d = new Date(r[1], r[2] - 1, r[3]);
 return (d.getFullYear() == r[1] && (d.getMonth() + 1) == r[2] && d
   .getDate() == r[3]);
}

Put the above code in an js file, then introduce the js file in the (jsp) page, and then call it


var checkIdCard = new IdCardValidate(id_card);
if(!checkIdCard.IsValid()){
  alert(' The ID card you entered is not in the correct format! ');
  return;
}

PS: Here are two very convenient regular expression tools for your reference:

JavaScript Regular Expression Online Test Tool:
http://tools.ofstack.com/regex/javascript

Regular expression online generation tool:
http://tools.ofstack.com/regex/create_reg

More readers interested in JavaScript can check out the topics of this site: "JavaScript Regular Expression Skills Encyclopedia", "JavaScript Replacement Operation Skills Summary", "JavaScript Search Algorithm Skills Summary", "JavaScript Data Structure and Algorithm Skills Summary", "JavaScript Traversal Algorithm and Skills Summary", "json Operation Skills Summary in JavaScript", "JavaScript Error and Debugging Skills Summary" and "JavaScript Mathematical Operation Usage Summary"

I hope this article is helpful to everyone's JavaScript programming.


Related articles: