Web Service学习笔记:15或18位身份证验证带输出省、生日、性别Web服务
学了一段时间的Web服务,今天利用网上的资源,做了一个15或18位身份证验证带输出省、市、区、生日、性别,主要是想练练手。现在把过程和心得分享给大家。
文章在我小站上的地址:Web Service学习笔记:15或18位身份证验证带输出省、生日、性别Web服务
Web服务地址:http://www.h2bbs.com/Weather/IDcard.asmx
直接发代码:
/// <summary>
/// IDcard 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.h2bbs.com/Weather/IDcard.asmx", Name = "IDcardWS", Description = "判断身份证号是否合法")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class IDcard : System.Web.Services.WebService
{
[WebMethod(Description = "输入15或18位身份证号码以验证其有效性")]
public idCardClass CheckCardidInfo(string cardid)
{
string[] strProvince = 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;
idCardClass cardidData = new idCardClass();
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$|^\d{15}$");
System.Text.RegularExpressions.Match mc = rg.Match(cardid);
if (!mc.Success)
{
cardidData.isValid = false;
cardidData.chkInfo = "不是有效的身份证号";
return cardidData;
}
if (cardid.Length == 15)
{
cardid = this.convert15to18(cardid);
cardid = cardid.ToLower();
cardid = cardid.Replace("x", "a");
}
if (strProvince[int.Parse(cardid.Substring(0, 2))] == null)
{
cardidData.isValid = false;
cardidData.chkInfo = "非法地区";
return cardidData;
}
try
{
DateTime.Parse(cardid.Substring(6, 4) + "-" + cardid.Substring(10, 2) + "-" + cardid.Substring(12, 2));
}
catch
{
cardidData.isValid = false;
cardidData.chkInfo = "非法生日";
return cardidData;
}
for (int i = 17; i >= 0; i--)
{
iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cardid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
}
if (iSum % 11 != 1)
{
cardidData.isValid = false;
cardidData.chkInfo = "非法证号";
return cardidData;
}
cardidData.isValid = true;
cardidData.chkInfo = strProvince[int.Parse(cardid.Substring(0, 2))] + "," + cardid.Substring(6, 4) + "年" + cardid.Substring(10, 2) + "月" + cardid.Substring(12, 2) + "日," + (int.Parse(cardid.Substring(16, 1)) % 2 == 1 ? "男" : "女");
return cardidData;
}
//将15位身份证号码转换为18位
private string convert15to18(string cardid)
{
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 = cardid.Substring(0, 6) + "19" + cardid.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 idCardClass
{
public idCardClass()
{
}
public bool isValid;
public string chkInfo;
}
}
/// IDcard 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.h2bbs.com/Weather/IDcard.asmx", Name = "IDcardWS", Description = "判断身份证号是否合法")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
public class IDcard : System.Web.Services.WebService
{
[WebMethod(Description = "输入15或18位身份证号码以验证其有效性")]
public idCardClass CheckCardidInfo(string cardid)
{
string[] strProvince = 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;
idCardClass cardidData = new idCardClass();
System.Text.RegularExpressions.Regex rg = new System.Text.RegularExpressions.Regex(@"^\d{17}(\d|x)$|^\d{15}$");
System.Text.RegularExpressions.Match mc = rg.Match(cardid);
if (!mc.Success)
{
cardidData.isValid = false;
cardidData.chkInfo = "不是有效的身份证号";
return cardidData;
}
if (cardid.Length == 15)
{
cardid = this.convert15to18(cardid);
cardid = cardid.ToLower();
cardid = cardid.Replace("x", "a");
}
if (strProvince[int.Parse(cardid.Substring(0, 2))] == null)
{
cardidData.isValid = false;
cardidData.chkInfo = "非法地区";
return cardidData;
}
try
{
DateTime.Parse(cardid.Substring(6, 4) + "-" + cardid.Substring(10, 2) + "-" + cardid.Substring(12, 2));
}
catch
{
cardidData.isValid = false;
cardidData.chkInfo = "非法生日";
return cardidData;
}
for (int i = 17; i >= 0; i--)
{
iSum += (System.Math.Pow(2, i) % 11) * int.Parse(cardid[17 - i].ToString(), System.Globalization.NumberStyles.HexNumber);
}
if (iSum % 11 != 1)
{
cardidData.isValid = false;
cardidData.chkInfo = "非法证号";
return cardidData;
}
cardidData.isValid = true;
cardidData.chkInfo = strProvince[int.Parse(cardid.Substring(0, 2))] + "," + cardid.Substring(6, 4) + "年" + cardid.Substring(10, 2) + "月" + cardid.Substring(12, 2) + "日," + (int.Parse(cardid.Substring(16, 1)) % 2 == 1 ? "男" : "女");
return cardidData;
}
//将15位身份证号码转换为18位
private string convert15to18(string cardid)
{
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 = cardid.Substring(0, 6) + "19" + cardid.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 idCardClass
{
public idCardClass()
{
}
public bool isValid;
public string chkInfo;
}
}
下面这个效果是我原来的带输出省、市、区的身份证号Web服务,但想想有点漏洞,而且他关联数据库,所以也就没发这个,上面的代码是有点啰嗦,园子里有人写过新的验证15位、18位的身份证号验证,大家可以去搜搜,不过他好像不能输出省。
作者:VisualStudio
出处:http://VisualStudio.cnblogs.com/
个人网站:H2站长论坛
本文版权归作者和博客园还有H2站长论坛共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://VisualStudio.cnblogs.com/
个人网站:H2站长论坛
本文版权归作者和博客园还有H2站长论坛共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。