MVVM根据身份证获取个人信息

下面给兄弟们演示一下如何根据身份证获取个人信息
先在vm里面声明一下身份证字段
//身份证
private string iDNumber;
public string IDNumber
{
get { return iDNumber; }
set
{
if (iDNumber != value)
{
iDNumber = value;
RaisePropertyChanged(“IDNumber”);
}
}
}
然后在页面文本框里面绑定一下
Text="{Binding IDNumber,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
记得双向绑定
给它添加文本改变事件
<i:Interaction.Triggers>
<i:EventTrigger EventName=“SelectionChanged”>
<i:InvokeCommandAction Command="{Binding idcartchang}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
去VM写命令
///
/// 身份证改变
///
public RelayCommand idcartchang { get; set; }
然后就是命令执行语句

public void idcard()
{
try
{
//提取地址
if (IDNumber.Length == 6)
{
string strAddress = CheckIDCardGetDiQu.LoadAddress(IDNumber);
//CheckIDCardGetDiQu是获取前面六位数地理信息的 上网找一大堆
if (strAddress == “”)
{
MessageBox.Show(“身份证不合法!”);
}
else
{
ContactAddress = strAddress;
}
}
//提取其他:性别年龄
if (IDNumber.Length == 18)
{
//闰年出生日期的合法性正则表达式 || 平年出生日期的合法性正则表达式
if (!Regex.IsMatch(IDNumber, @"(1[0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx] ) " ) ∣ ∣ ! R e g e x . I s M a t c h ( I D N u m b e r , @ " ( [ 1 − 9 ] [ 0 − 9 ] 5 ( 19 ∣ 20 ) [ 0 − 9 ] 2 ( ( 01 ∣ 03 ∣ 05 ∣ 07 ∣ 08 ∣ 10 ∣ 12 ) ( 0 [ 1 − 9 ] ∣ [ 1 − 2 ] [ 0 − 9 ] ∣ 3 [ 0 − 1 ] ) ∣ ( 04 ∣ 06 ∣ 09 ∣ 11 ) ( 0 [ 1 − 9 ] ∣ [ 1 − 2 ] [ 0 − 9 ] ∣ 30 ) ∣ 02 ( 0 [ 1 − 9 ] ∣ 1 [ 0 − 9 ] ∣ 2 [ 0 − 8 ] ) ) [ 0 − 9 ] 3 [ 0 − 9 X x ] )") || !Regex.IsMatch(IDNumber, @"(^[1-9][0-9]{5}(19|20)[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx] )")!Regex.IsMatch(IDNumber,@"([19][09]5(1920)[09]2((01030507081012)(0[19][12][09]3[01])(04060911)(0[19][12][09]30)02(0[19]1[09]2[08]))[09]3[09Xx])"))
{
MessageBox.Show(“身份证不合法!”);
IDNumber = “”;
}
else
{
string keys = IDNumber;
//性别
int sex = int.Parse(keys.Substring(16, 1));
//年
string birth_y = keys.Substring(6, 4);
//月
string birth_m = keys.Substring(10, 2);
//日
string birth_d = keys.Substring(12, 2);
ListViewItem l = new ListViewItem();
//绑定出生日期
DateBirth = Convert.ToDateTime(birth_y + “/” + birth_m + “/” + birth_d);
//获取今年年份
string strNow = DateTime.Now.Year.ToString();
//获取今月月份
string strNowm = DateTime.Now.Month.ToString();
//获取今日日
string strNowd = DateTime.Now.Date.ToString();
//把今月转化成数字
decimal decNowm = Convert.ToDecimal(strNowm);
//获取身份证的出生月份
decimal debirth_m = Convert.ToDecimal(birth_m);
//获取身份证的出生日
decimal debirth_d = Convert.ToDecimal(birth_d);
//把今年转化成数字
decimal decNow = Convert.ToDecimal(strNow);
//获取(截取身份证)出生年份
decimal decbirth_y = Convert.ToDecimal(birth_y);
//获取虚岁
decimal decAge = Convert.ToDecimal(decNow - decbirth_y) + 1;
//绑定年龄Infant
years = decAge.ToString().Trim();
//婴儿否
if (Convert.ToInt32(years) < 3)
{
Infant = true;
}
else
{
Infant = false;
}
//获取月份
//如果当前月份大于出生月份 岁数减1
if (debirth_m < decNowm)
{
decimal decmo = Convert.ToDecimal(decNowm- debirth_m);
month = Convert.ToString(decmo);
}
else
{
years = Convert.ToString(Convert.ToInt32(decAge.ToString().Trim()) -1);
decimal a =debirth_m- decNowm;
decimal mo = 12 - a;
//获取出生月份
month = Convert.ToString(mo);
}
//取余
if (sex % 2 == 0)
{
//l.SubItems.Add(“女”);
gender = “女”;//
} //if
else
{
//l.SubItems.Add(“男”);
gender = “男”;//
}
}
}
}
catch (Exception)
{
}
}


  1. 1-9 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值