填写身份证自动匹配相应的信息
开发工具与关键技术:Visual Studio 2015、WPF
作者:郑伟基
撰写时间:2019年6月7日
下面我们使用了一个正则表达式来来匹配正确的身份证号码,,输入身份证号码它会自动匹配里面相对应的身份证号码信息,会匹配出身日期、自动计算年龄、性别,我还在身份证的文本框里面限制了身份证的长度限制,因为大多数的身份证都是18位的,见下面的XAML代码表达。
见下面的XAML代码:
<Grid>
<Border Background="AliceBlue">
<GroupBox Margin="5" Header="填写身份证自动匹配相应的信息">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="22*" />
<RowDefinition Height="25*" />
<RowDefinition Height="35*" />
<RowDefinition Height="25*" />
<RowDefinition Height="35*" />
<RowDefinition Height="25" />
<RowDefinition Height="35"/>
<RowDefinition Height="25"/>
<RowDefinition Height="61"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="29*" />
<ColumnDefinition Width="52*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="29*" />
<ColumnDefinition Width="52*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="1" Text="身份证号码:" VerticalAlignment="Center" Height="15" Margin="0,5" />
<TextBox Grid.Row="1" Grid.Column="1" x:Name="txt_ShenFenZheng" TextChanged="ShenFenZheng_TextChanged" VerticalContentAlignment="Center" MaxLength="18"/>
<TextBlock Grid.Row="1" Grid.Column="3" Text="出身日期:" VerticalAlignment="Center" Height="15" Margin="0,5" />
<DatePicker Grid.Row="1" Grid.Column="4" x:Name="txt_ChuShengRiQi" />
<TextBlock Grid.Row="3" Text="年龄:" VerticalAlignment="Center" Height="16" Margin="0,4" />
<TextBox Grid.Row="3" Grid.Column="1" x:Name="txt_NianLing" VerticalContentAlignment="Center" />
<TextBlock Grid.Row="3" Grid.Column="3" Text="性别:" VerticalAlignment="Center" Height="16" Margin="0,4"/>
<TextBox Grid.Row="3" Grid.Column="4" x:Name="txt_XingBie" VerticalContentAlignment="Center" />
<TextBlock Grid.Row="5" Text="身份证地址:" VerticalAlignment="Center" Margin="0,5,0,4" />
<TextBox Grid.Row="5" Grid.Column="1" x:Name="txt_LianXiDiZhi" VerticalContentAlignment="Center" />
<Button Grid.Row="7" Grid.Column="4" x:Name="btn_1" Content="清空" Width="80" BorderBrush="SkyBlue" Foreground="White" Background="SkyBlue" Click="btn_1_Click"/>
</Grid>
</GroupBox>
</Border>
</Grid>
见下面的C#代码:
using System;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using 身份证验证并回填.PublicStyles;
namespace 身份证验证并回填
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// 身份证改变事件(验证有效身份证)
private void ShenFenZheng_TextChanged(object sender, TextChangedEventArgs e)
{
string ShengFenZheng = txt_ShenFenZheng.Text.Trim();
try
{
if(ShengFenZheng.Length == 18)
{
if (!Regex.IsMatch(ShengFenZheng, @"(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)"))
{
MessageBox.Show("身份证不合法!");
txt_ShenFenZheng.Text = "";
}
else
{
//截取年:Substring
string birth_y = ShengFenZheng.Substring(6, 4);
//截取月:Substring
string birth_m = ShengFenZheng.Substring(10, 2);
//截取日:Substring
string birth_d = ShengFenZheng.Substring(12, 2);
ListViewItem l = new ListViewItem();
//绑定出生日期
txt_ChuShengRiQi.Text = birth_y + "年" + birth_m + "月" + birth_d + "日";
//获取今年的 年份
string strNow = DateTime.Now.Year.ToString();
//把今年的年份转换成数字
decimal decNow = Convert.ToDecimal(strNow);
//截取身份证的出生年份
decimal decimal_y = Convert.ToDecimal(birth_y);
//获取虚岁
decimal decAge = Convert.ToDecimal(decNow - decimal_y) + 1;
//绑定年龄
txt_NianLing.Text = decAge.ToString().Trim();
//截取身份证的第16位的后一位数
int XingBie = int.Parse(ShengFenZheng.Substring(16, 1));
//绑定性别
if (XingBie % 2 ==0)
{
txt_XingBie.Text = "女";
}
else
{
txt_XingBie.Text = "男";
}
}
}
}
catch (Exception)
{
throw;
}
//使用封装好的方法
if (txt_ShenFenZheng.Text.ToString().Length == 6)
{
string strLianXiDiZhi = CheckIDCardGetDiQu.LoadAddress(txt_ShenFenZheng.Text.ToString());
if (strLianXiDiZhi == "")
{
MessageBox.Show("身份证不合法!");
}
else
{
//绑定身份证地址
txt_LianXiDiZhi.Text = strLianXiDiZhi;
}
}
if (txt_ShenFenZheng.Text.ToString().Length < 6)
{
txt_LianXiDiZhi.Text = "";
}
}
//清空按钮
private void btn_1_Click(object sender, RoutedEventArgs e)
{
//弹出确定对话框
MessageBoxResult dr =
MessageBox.Show("是否要清空数据!", "系统提示", MessageBoxButton.OKCancel,MessageBoxImage.Information);
if (dr == MessageBoxResult.OK) //如果点了确定按钮
{
//清空数据
txt_ShenFenZheng.Text = string.Empty;
txt_ChuShengRiQi.Text = string.Empty;
txt_NianLing.Text = string.Empty;
txt_XingBie.Text = string.Empty;
txt_LianXiDiZhi.Text = string.Empty;
}
}
}
}
在截图下面的截图中看到我写进身份证的信息,其他相关的信息也会从身份证号码里面自动去匹配相应的信息
见下面的效果截图: