调用isIdCardNo(num)  验证通过返回true 错误返回false

function isIdCardNo(num) {
var factorArr = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1);
var parityBit = new Array("1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2");
var varArray = new Array();
var intValue;
var lngProduct = 0;
var intCheckDigit;
var intStrLen = num.length;
var idNumber = num;
// initialize
if ((intStrLen != 15) && (intStrLen != 18)) {
return false;
}
// check and set value
for (i = 0; i < intStrLen; i++) {
varArray[i] = idNumber.charAt(i);
if ((varArray[i] < '0' || varArray[i] > '9') && (i != 17)) {
return false;
} else if (i < 17) {
varArray[i] = varArray[i] * factorArr[i];
}
} if (intStrLen == 18) {
//check date
var date8 = idNumber.substring(6, 14);
if (isDate8(date8) == false) {
return false;
}
// calculate the sum of the products
for (i = 0; i < 17; i++) {
lngProduct = lngProduct + varArray[i];
}
// calculate the check digit
intCheckDigit = parityBit[lngProduct % 11];
// check last digit
if (varArray[17] != intCheckDigit) {
return false;
}
}
else { //length is 15
//check date
var date6 = idNumber.substring(6, 12);
if (isDate6(date6) == false) {
return false;
}
}
return true;
}
function isDate6(sDate) {
if (!/^[0-9]{6}$/.test(sDate)) {
return false;
}
var year, month, day;
year = sDate.substring(0, 4);
month = sDate.substring(4, 6);
if (year < 1700 || year > 2500) return false
if (month < 1 || month > 12) return false
return true
}
/**
* 判断是否为“YYYYMMDD”式的时期
*
*/
function isDate8(sDate) {
if (!/^[0-9]{8}$/.test(sDate)) {
return false;
}
var year, month, day;
year = sDate.substring(0, 4);
month = sDate.substring(4, 6);
day = sDate.substring(6, 8);
var iaMonthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if (year < 1700 || year > 2500) return false
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) iaMonthDays[1] = 29;
if (month < 1 || month > 12) return false
if (day < 1 || day > iaMonthDays[month - 1]) return false
return true
}

  

isIdCardNo

最新文章

  1. 2015年可用的TRACKER服务器大全
  2. java 动态创建数据库和动态连接数据库
  3. springMvc配置编码过滤器
  4. 常见的mongo shell命令
  5. [AngularJS] Directive Definition Objects (DDO)
  6. ASP.Net Core-依赖注入IoC
  7. IPCS资源
  8. 添加站点图标: 为SAE上的WordPress站点添加自己的Favicon
  9. 在OpenWrt上编写自己的硬件操作程序
  10. python作业03-文件操作&amp;函数
  11. HDTV(1920x1080)码率和视频质量关系的研究 1 (前期准备)
  12. [Day16]常用API(正则表达式、Date类、DateFormat类、Calendar类)
  13. android项目生成aar和引用aar解决方案
  14. 【Dubbo&amp;&amp;Zookeeper】3、Failed to read schema document &#39;http://code.alibabatech.com/schema/dubbo/dubbo.xsd&#39;问题解决方法
  15. ThinkPHP 3.2 用户注册邮箱验证激活帐号
  16. JDBC连接数据库的简单介绍
  17. 4月24 php基础及函数的应用
  18. QString 和char数组转换(转)
  19. Linux CentOS6.6 NFS服务的配置与安装
  20. A Good Story for Great Friends

热门文章

  1. CF45G
  2. 洛谷 P3580 - [POI2014]ZAL-Freight(单调队列优化 dp)
  3. Codeforces 1466G - Song of the Sirens(哈希)
  4. Codeforces 1491G - Switch and Flip(构造题)
  5. 【低门槛 手把手】python 装饰器(Decorators)原理说明
  6. VSCode + PicGo + Github + jsDelivr 搭建稳定快速高效图床
  7. Oracle-判断一个表的一列是否在另一张表的一列存在
  8. php代码审计入门前必看
  9. C语言中的重要位运算
  10. 巩固javaweb第十五天