(- -)功能说明:
要求:根据以下规则对身份证号码进行验证,运行效果如图3-7所示。
(1)号码长度18 位。
(2) 18位全是数字。
(3)第7~ 10位是出生的年。
(4)倒数第2位号码,奇数为男性,偶数为女性。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace IdCheck
{
public partial class Id : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string s = TextBox1.Text;
if (s.Length != 18)
Label2.Text = "输入了非18位的数字";
else
{
int k = 0;
System.Text.ASCIIEncoding ascii = new System.Text.ASCIIEncoding();
byte[] bytest = ascii.GetBytes(s);//字节是二进制数组的单位,不会浪费空间
foreach (var it in bytest)
{
if (it < 48 || it > 57)
{
Label2.Text = "输入了非法字符";
k = 1;
}
}
if (k == 0)
{
string year, month, day;
year = s.Substring(6, 4);
month= s.Substring(10, 2);
day= s.Substring(12, 2);
Label2.Text="您出生于 "+year+"年"+month+"月"+day+"日";
if (bytest[16]%2==1)
{
Label2.Text += ",您的性别是 " + "男";
}
else
Label2.Text += ",您的性别是 " + "女";
}
}
}
}
}