身份证号码验证web服务(完整源码)

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體: |||超大  

身份证号码验证web服务(完整源码)

身份证号码验证web服务(完整源码) 已经有18位号的c#程序和15位到18位转换的程序但还没有一个完整的服务器端验证程序所以加以总结了一下写了一个做为web服务供大家交流使用

<%@ WebService Language='c#' %>

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Web;

using System.Web.Services;

namespace WeatherWS

{

/// <summary>

/// checkID 的摘要说明。

/// </summary>

[WebService(Namespace='http://flying.redv.com/monster')]

public class checkCID : System.Web.Services.WebService

{

public checkCID()

{

InitializeComponent();

}

#region 组件设计器生成的代码

//Web 服务设计器所必需的

private IContainer components = null;

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if(disposing && components != null)

{

components.Dispose();

}

base.Dispose(disposing);

}

#endregion

[WebMethod(Description='输入15或18位身份证号码以验证其有效性')]

public idDataClass CheckCidInfo(string cid)

{

string[] aCity = new string[]{null,null,null,null,null,null,null,null,null,null,null,'北京','天津','河北','山西','内蒙古',null,null,null,null,null,'辽宁','吉林','黑龙江',null,null,null,null,null,null,null,'上海','江苏','浙江','安微','福建','江西','山东',null,null,null,'河南','湖北','湖南','广东','广西','海南',null,null,null,'重庆','四川','贵州','云南','西藏',null,null,null,null,null,null,'陕西','甘肃','青海','宁夏','新疆',null,null,null,null,null,'台湾',null,null,null,null,null,null,null,null,null,'香港','澳门',null,null,null,null,null,null,null,null,'国外'};

double iSum=0;

idDataClass _cidData = new idDataClass();

System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@'^\d{17}(\d|x)$|^\d{15}$');

System.Text.RegularExpressions.Match mc = rg.Match(cid);

if(!mc.Success)

{

_cidData.isValid = false;

_cidData.chkInfo = '不是有效的身份证号';

return _cidData;

}

if(cid.Length==15) cid = this.convert15to18(cid);

cid = cid.ToLower();

cid = cid.Replace('x','a');

if(aCity[int.Parse(cid.Substring(0,2))]==null)

{

_cidData.isValid = false;

_cidData.chkInfo = '非法地区';

return _cidData;

}

try

{

DateTime.Parse(cid.Substring(6,4)+'-'+cid.Substring(10,2)+'-'+cid.Substring(12,2));

}

catch

{

_cidData.isValid = false;

_cidData.chkInfo = '非法生日';

return _cidData;

}

for(int i=17;i>=0;i--)

{

iSum +=(System.Math.Pow(2,i)%11)*int.Parse(cid[17-i].ToString(),System.Globalization.NumberStyles.HexNumber);

}

if(iSum%11!=1)

{

_cidData.isValid = false;

_cidData.chkInfo = '非法证号';

return _cidData;

}

_cidData.isValid = true;

_cidData.chkInfo = aCity[int.Parse(cid.Substring(0,2))]+','+cid.Substring(6,4)+'-'+cid.Substring(10,2)+'-'+cid.Substring(12,2)+','+(int.Parse(cid.Substring(16,1))%2==1?'男':'女');

return _cidData;

}

//将15位身份证号码转换为18位

private string convert15to18(string cid)

{

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

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

string strTemp;

int intTemp = 0;

strTemp = cid.Substring(0,6) + '19' + cid.Substring(6);

for (int i=0;i<=strTemp.Length-1;i++)

{

intTemp += int.Parse(strTemp.Substring(i,1))*intQuan[i];

}

intTemp = intTemp % 11;

return strTemp + strJiaoYan[intTemp];

}

}

//用于保存身份证验证结果的类

public class idDataClass

{

public idDataClass()

{

}

public bool isValid;

public string chkInfo;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航