身份证号码有效性检测算法 ( js版 转 C#版 )

摘要:
Regex.IsMatch){returnfalse;}intyear,month;year=Convert.ToInt32;month=Convert.ToInt32;ifreturnfalse;ifreturnfalse;returntrue;}//////18位身份证日期检测--年/月/日--8位/////////staticboolisDate8{if(!

C#版

#region 检测是否是正确的身份证

/// <summary>

/// 身份证验证

/// </summary>

/// <param name="num"></param>

/// <returns></returns>

public static bool isIdCardNo(string cardid)

{

string num = cardid.ToUpper();

int[] factorArr = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };

char[] parityBit = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };

char[] varArray = new char[18];

var lngProduct = 0;

int intCheckDigit = 0;

var intStrLen = num.Length;

var idNumber = num;

// initialize

if ((intStrLen != 15) && (intStrLen != 18))

{

return false;

}

// check and set value

for (int i = 0; i < intStrLen; i++)

{

varArray[i] = (char)Convert.ToInt32(idNumber[i]);

if (((varArray[i] - 48) < 0 || (varArray[i] - 48) > 9) && (i != 17))

{

return false;

}

else if (i < 17)

{

varArray[i] = (char)((varArray[i] - 48) * factorArr[i]);

}

}

if (intStrLen == 18)

{

//check date

var date8 = idNumber.Substring(6, 8);

if (isDate8(date8) == false)

{

return false;

}

// calculate the sum of the products

for (int 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, 6);

if (isDate6(date6) == false)

{

return false;

}

}

return true;

}

/// <summary>

/// 15位身份证日期检测--年/月--6位

/// </summary>

/// <param name="sDate"></param>

/// <returns></returns>

static bool isDate6(string sDate)

{

if (!Regex.IsMatch(sDate, "^[0-9]{6}$"))

{

return false;

}

int year, month;

year = Convert.ToInt32(sDate.Substring(0, 4));

month = Convert.ToInt32(sDate.Substring(4, 2));

if (year < 1700 || year > 2500) return false;

if (month < 1 || month > 12) return false;

return true;

}

/// <summary>

/// 18位身份证日期检测--年/月/日--8位

/// </summary>

/// <param name="sDate"></param>

/// <returns></returns>

static bool isDate8(string sDate)

{

if (!Regex.IsMatch(sDate, "^[0-9]{8}$"))

{

return false;

}

int year, month, day;

year = Convert.ToInt32(sDate.Substring(0, 4));

month = Convert.ToInt32(sDate.Substring(4, 2));

day = Convert.ToInt32(sDate.Substring(6, 2));

int[] iaMonthDays = new int[] { 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;

}

#endregion

二、JS版

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

}

免责声明:文章转载自《身份证号码有效性检测算法 ( js版 转 C#版 )》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇如何使iframe外部的超级链接的页面在iframe中打开a标签链接html状态码下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

关于a标签不能调用js方法的小细节,你注意到了么?

在我们做后台删除的时候,当点击删除标签时,你希望弹出一个友好的提示框!比如这样: 那代码应该怎样写呢?向下面这样? <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <script> wi...

如何利用JS检查元素是否在视口内

前言 分享两个监测元素是否在视口内的方法 1. 位置计算 使用 Element.getBoundingClientRect() 方法返回元素相对于视口的位置 const isElementVisible = (el) => {const rect = el.getBoundingClientRect();}; 获取浏览器窗口的宽高 const isE...

Webpack安装配置及打包详细过程

引言 前端经过漫长的发展,涌现出了很多实践方法来处理复杂的工作流程,让开发变得更加简便,其中,模块化可以使复杂的程序细化成为各个小的文件,而webpack并不强制你使用某种模块化方案,而是通过兼容所有模块化方案让你无痛接入项目,本文详细介绍webpack安装配置及打包的详细过程。 Webpack简单介绍 本质上,webpack是一个现代 JavaScrip...

零开始Android逆向教程(一)——初探Android逆向

 这段时间因为某些业务驱动,开始研究一些逆向相关的东西,浏览了下其所包含的大致内容,发现真是一个新大陆,跟之前耳听目染过的一些门面介绍完全不是一个层级的,真正的印证了下手难这一说法。   谨此以本文开始记录我的Android逆向之旅吧。总述   习惯于应用层开发的我们都知道,在应用上架的时候都需要程序经过编译、签名 、生成一个后缀为apk的文件才能发布到应...

【JS笔记】5.3 Date类型

Date类型存储的信息:从UTC(1970年1月1日0时)开始经过的毫秒数 创建Date对象: 使用构造函数Date() 表示当前毫秒数的Date对象:var now = new Date();//不用参数时默认为当前毫秒数 表示指定毫秒数的Date对象:var date1 = new Date(milliseconds); 获取milliseconds方...

通过Url Protocol实现web调用本地exe,兼容谷歌IE,并实现本地验证

1.随便在网上找个注册码写入,web调用方法为<a href="openLayerManager:">调用</a> 一定要注意后面的冒号 2.这样调用的话你会发现,如果本地没有该程序,那么你怎么点击都不会有反应,这样就需要本地exe程序有无验证 3.验证,首先需要下载两个大神写的js文件example.js和protocolchec...