题目:对输入框中输入的身份证是否合法进行验证:
1)使用正则表达式对格式进行验证(共18位,前17位为数字,最后1位是数字或字母,其中年月日部分也可以进行验证;
2)对身份证的最后一位的有效性进行验证。
最后一位校验规则:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApp17
{
class Program
{
static void Parseid(string id)
{
string pattern = @"^\d{17}(?:\d|X)$";
string birth = id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
DateTime time = new DateTime();
int[] arr_weight = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; // 加权数组
string[] id_last = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" }; // 校验数组
int sum = 0;
for (int i = 0; i < 17; i++)
{
sum += arr_weight[i] * int.Parse(id[i].ToString());
}
int result = sum % 11; // 实际校验位的值
if (Regex.IsMatch(id, pattern)) // 18位格式检查
{
if(DateTime.TryParse(birth, out time)) // 出生日期检查
{
if (id_last[result] == id[17].ToString()) // 校验位检查
{
Console.WriteLine("身份证号格式正确!");
}
else
{
Console.WriteLine("最后一位校验错误!");
}
}
else
{
Console.WriteLine("出生日期验证失败!");
}
}
else
{
Console.WriteLine("身份证号格式错误!");
}
}
static void Main(string[] args)
{
string id;
id = Console.ReadLine();
Parseid(id);
Console.Read();
}
}
}
运行结果我就不放了,拿你自己身份证号验证一下吧。