身份证号码验证web服务

<%@ WebService Language="c#"  Class="WeatherWS.checkCID" %>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WeatherWS
{
///
/// checkID 的摘要说明。
///

[WebService(Namespace="http://flying.redv.com/monster")]
public class checkCID : System.Web.Services.WebService
{
public checkCID()
{
InitializeComponent();
}

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

//Web 服务设计器所必需的
private IContainer components = null;

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

private void InitializeComponent()
{
}

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

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 ;
}
intTemp = intTemp % 11;
return strTemp + strJiaoYan[intTemp];
}

}
//用于保存身份证验证结果的类
public class idDataClass
{
public idDataClass()
{
}
public bool isValid;
public string chkInfo;
}
}

你可能感兴趣的:(Web)