最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Node.js通過身份證號驗證年齡、出生日期與性別方法示例

 更新時間:2017年03月09日 11:54:16   投稿:daisy  
最近工作中需要對身份證號的年齡、出生日期與性別進行驗證,所以這篇文章主要介紹了Node.js通過身份證號驗證年齡、出生日期與性別的方法,在介紹完node.js的實現(xiàn)方法后又給大家分類的利用JS實現(xiàn)的方法,需要的朋友可以參考下。

前言

大家如果想要知道自己的年齡,出生日期和性別,或者是別人的,給我個身份證號,我就可以知道,其實很簡單的,看下面代碼。

node.js實現(xiàn)

static validateIdNumberToAgeYear(str){
 let date = new Date();
 let currentYear = date.getFullYear();
 let currentMonth = date.getMonth() + 1;
 let currentDate = date.getDate();
 
 let idxSexStart = str.length == 18 ? 16 : 14;
 let birthYearSpan = str.length == 18 ? 4 : 2;

 let year;
 let month;
 let day;
 let sex;
 let birthday;
 let age;

 //性別
 let idxSex = 1 - str.substr(idxSexStart, 1) % 2; 
 sex = idxSex == '1' ? '女' : '男'; 
 //生日
 year = (birthYearSpan == 2 ? '19' : '') + str.substr(6, birthYearSpan); 
 month = str.substr(6 + birthYearSpan, 2); 
 day = str.substr(8 + birthYearSpan, 2); 
 birthday = year + '-' + month + '-' + day; 
 //年齡
 let monthFloor = (currentMonth < parseInt(month,10) || (currentMonth == parseInt(month,10) && currentDate < parseInt(day,10))) ? 1 : 0;
 age = currentYear - parseInt(year,10) - monthFloor; 

 // console.log("我的出生日期是"+year+"年"+month+"月"+day+"日"+",今年"+age+"歲了"+",性別是"+sex);

 if(age >= 18){
  return true; 
 }
 
 return false;
}

我這里只是做了一個年齡的判斷。

利用js也可以實現(xiàn)

1. 自定義js類如下:

// 構(gòu)造函數(shù),變量為15位或者18位的身份證號碼 
function clsIDCard(CardNo) { 
 this.Valid = false; 
 this.ID15 = ''; 
 this.ID18 = ''; 
 this.Local = ''; 
 if (CardNo != null) 
  this.SetCardNo(CardNo); 
} 


// 設置身份證號碼,15位或者18位 
clsIDCard.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); 
} 
// 校驗身份證有效性 
clsIDCard.prototype.IsValid = function() { 
 return this.Valid; 
} 
// 返回生日字符串,格式如下,1981-10-10 
clsIDCard.prototype.GetBirthDate = function() { 
 var BirthDate = ''; 
 if (this.Valid) 
  BirthDate = this.GetBirthYear() + '-' + this.GetBirthMonth() + '-' 
    + this.GetBirthDay(); 
 return BirthDate; 
} 
// 返回生日中的年,格式如下,1981 
clsIDCard.prototype.GetBirthYear = function() { 
 var BirthYear = ''; 
 if (this.Valid) 
  BirthYear = this.ID18.substr(6, 4); 
 return BirthYear; 
} 
// 返回生日中的月,格式如下,10 
clsIDCard.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 
clsIDCard.prototype.GetBirthDay = function() { 
 var BirthDay = ''; 
 if (this.Valid) 
  BirthDay = this.ID18.substr(12, 2); 
 return BirthDay; 
} 

// 返回性別,1:男,0:女 
clsIDCard.prototype.GetSex = function() { 
 var Sex = ''; 
 if (this.Valid) 
  Sex = this.ID18.charAt(16) % 2; 
 return Sex; 
} 

// 返回15位身份證號碼 
clsIDCard.prototype.Get15 = function() { 
 var ID15 = ''; 
 if (this.Valid) 
  ID15 = this.ID15; 
 return ID15; 
} 

// 返回18位身份證號碼 
clsIDCard.prototype.Get18 = function() { 
 var ID18 = ''; 
 if (this.Valid) 
  ID18 = this.ID18; 
 return ID18; 
} 

// 返回所在省,例如:上海市、浙江省 
clsIDCard.prototype.GetLocal = function() { 
 var Local = ''; 
 if (this.Valid) 
  Local = this.Local; 
 return Local; 
} 

clsIDCard.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]; 
} 

clsIDCard.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 : "內(nèi)蒙古", 
  21 : "遼寧", 
  22 : "吉林", 
  23 : "黑龍江 ", 
  31 : "上海", 
  32 : "江蘇", 
  33 : "浙江", 
  34 : "安徽", 
  35 : "福建", 
  36 : "江西", 
  37 : "山東", 
  41 : "河南", 
  42 : "湖北 ", 
  43 : "湖南", 
  44 : "廣東", 
  45 : "廣西", 
  46 : "海南", 
  50 : "重慶", 
  51 : "四川", 
  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; 
} 

clsIDCard.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]); 
}

2. 頁面只需要new出對象,并傳遞數(shù)據(jù)驗證,并可獲得相關數(shù)據(jù)( 住址 | 出生日期 | 性別 )即可:

$("#cardNo").blur(function(event){ 
  var idCard = $(this).val(); 
   
  var checkFlag = new clsIDCard(idCard);  
  if( !checkFlag.IsValid() ){ 
   alert("身份證錯誤"); 
   return false; 
  }else{ 
   alert( "出生于: " + checkFlag.GetBirthDate() +" 地區(qū):" + checkFlag.GetLocal() +" sex:" + checkFlag.GetSex() ); 
  }   
 });

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • node(koa2) web應用模塊介紹詳解

    node(koa2) web應用模塊介紹詳解

    這篇文章主要介紹了node(koa2) web應用模塊介紹詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-03-03
  • node-red File讀取好保存實例講解

    node-red File讀取好保存實例講解

    在本篇文章里小編給大家整理了關于node-red File讀取好保存的相關知識點內(nèi)容,有需要的朋友們可以參考下。
    2019-09-09
  • 修改NPM全局模式的默認安裝路徑的方法

    修改NPM全局模式的默認安裝路徑的方法

    這篇文章主要介紹了修改NPM全局模式的默認安裝路徑的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • websocket+node.js實現(xiàn)實時聊天系統(tǒng)問題咨詢

    websocket+node.js實現(xiàn)實時聊天系統(tǒng)問題咨詢

    最近新學習websocket,做了一個實時聊天。用Node.js搭建的服務:serevr.js. 兩個相互通信頁面:client.html 和server.html 但是就是有很多問題,下面通過本文給大家分享下
    2017-05-05
  • Node文件操作匯總實例詳解

    Node文件操作匯總實例詳解

    這篇文章主要為大家介紹了Node文件操作匯總實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-10-10
  • Node.js使用SQLite數(shù)據(jù)庫方法大全

    Node.js使用SQLite數(shù)據(jù)庫方法大全

    Node.js是一種流行的JavaScript運行時,提供了許多有用的模塊和庫來構(gòu)建Web應用程序,而SQLite是一種嵌入式關系型數(shù)據(jù)庫,它可以運行在各種操作系統(tǒng)上,包括Windows、Linux和Mac OS X等,在Node.js中,可以通過安裝sqlite3模塊來訪問SQLite數(shù)據(jù)庫
    2023-10-10
  • Node.js中的進程間通信

    Node.js中的進程間通信

    這篇文章主要介紹了Node.js中的進程間通信,文章圍繞主題展開詳細的內(nèi)容戒殺,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-09-09
  • npm run dev和npm run serve的區(qū)別小結(jié)

    npm run dev和npm run serve的區(qū)別小結(jié)

    npm run serve和npm run dev是在開發(fā)階段使用npm運行腳本的兩種常見命令,本文就來介紹一下這兩者的區(qū)別,具有一定的參考價值,感興趣的可以了解一下
    2024-03-03
  • node.js使用redis儲存session的方法

    node.js使用redis儲存session的方法

    這篇文章主要介紹了node.js使用redis儲存session的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-09-09
  • 使用nodejs搭建微信小程序支付接口的詳細過程

    使用nodejs搭建微信小程序支付接口的詳細過程

    前段時間做微信支付,遇到了很多坑,網(wǎng)上也沒有講解的特別明白的,通過借鑒各路人才的經(jīng)驗,最后也完成了,下面這篇文章主要給大家介紹了關于使用nodejs搭建微信小程序支付接口的詳細過程,需要的朋友可以參考下
    2022-12-12

最新評論

耒阳市| 景泰县| 赤壁市| 沅江市| 洞头县| 湟中县| 井研县| 宜章县| 廊坊市| 玉田县| 荃湾区| 天津市| 健康| 泰宁县| 饶阳县| 溆浦县| 辉县市| 天门市| 阿坝县| 孟州市| 丽水市| 荆州市| 绥滨县| 定安县| 朝阳县| 临泽县| 诸城市| 鹤庆县| 盘锦市| 曲水县| 竹山县| 肃宁县| 津市市| 瑞金市| 芦山县| 贵溪市| 杭锦后旗| 浦北县| 长丰县| 金阳县| 南城县|