实现身份证的判定 前17位都是数字最后一位X或者x 18位都是数字 查出出生年月性别
if (txtId.Text.Length!= 18)
{//18位身份证号码
Response.Write("<script>alert('请输入18位身份证号码!')</script>");
return;
}else {
// 18位全是数字
System.Text.ASCIIEncoding asci = new System.Text.ASCIIEncoding();
byte[] bystr = asci.GetBytes(txtId.Text);
// 第18位是否为x或者X
if (bystr[17]==88 || bystr[17]==120){
//前17位都是数字
for (int i = 0; i < bystr.Length - 1; i++){
if (bystr[i] < 48 || bystr[i] > 57)
{
Response.Write("<script>alert('前17位都是数字!')</script>");
return;
}else
{
//第7-10位是出身的年
// 倒数第二位号码奇数为男偶数为女
string sex;
string year = txtId.Text.Substring(6, 4);
if (bystr[16] % 2 == 0)
{
sex = "男";
}
else
{
sex = "女";
}
//用一个label控件接收
lblBoth.Text = "您是" + year + "出生的" + "性别为:" + sex;
}
}
}
else {
//如果18位都是数字
for (int i = 0; i < bystr.Length; i++){
if (bystr[i] < 48 || bystr[i] > 57)
{
Response.Write("<script>alert('18位都是数字!')</script>");
return;
}else
{
//第7-10位是出身的年
// 倒数第二位号码奇数为男偶数为女
string sex;
string year = txtId.Text.Substring(6, 4);
if (bystr[16] % 2 == 0)
{
sex = "男";
}
else
{
sex = "女";
}
lblBoth.Text = "您是" + year + "出生的" + "性别为:" + sex;
}
}
}
}