ASP.NET学习社区ASP.NET学习区ASP.NET文摘 .NET中对18位身份证进行验证的方法

1  /  1  页   1 跳转 查看:666

.NET中对18位身份证进行验证的方法

.NET中对18位身份证进行验证的方法

18位身份证验证方法

    Private Function CheckCidInfo(ByVal cid As String) As String

        Dim aCity() As String = {"", "", "", "", "", "", "", "", "", "", _

        "", "北京", "天津", "河北", "山西", "内蒙古", "", _

        "", "", "", "", "辽宁", "吉林", "黑龙江", "", "", _

        "", "", "", "", "", "上海", "江苏", "浙江", "安微", _

        "福建", "江西", "山东", "", "", "", "河南", "湖北", _

        "湖南", "广东", "广西", "海南", "", "", "", "重庆", _

        "四川", "贵州", "云南", "西藏", "", "", "", "", "", _

        "", "陕西", "甘肃", "青海", "宁夏", "新疆", "", "", _

        "", "", "", "台湾", "", "", "", "", "", "", "", "", _

        "", "香港", "澳门", "", "", "", "", "", "", "", "", "国外"}

        Dim iSum As Double = 0

        Dim info As String = ""

        Dim rg As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex("^\d{17}(\d|x)$")

        Dim mc As System.Text.RegularExpressions.Match = rg.Match(cid)

        If mc.Success = False Then

            Return ""

        End If

        cid = cid.ToLower

        cid = cid.Replace("x", "a")

        If aCity(Integer.Parse(cid.Substring(0, 2))) = "" Then

            Return "非法地址"

        End If

        Try

            DateTime.Parse(cid.Substring(6, 4) & "-" & cid.Substring(10, 2) & "-" & cid.Substring(12, 2))

        Catch ex As Exception

            Return "非法生日"

        End Try

        Dim i As Integer

        For i = 17 To 0 Step -1

            iSum = iSum + (System.Math.Pow(2, i) Mod 11) * Integer.Parse(cid.Substring(17 - i, 1), System.Globalization.NumberStyles.HexNumber)

            '   

        Next

        If iSum Mod 11 <> 1 Then

            Return "非法证号"

        End If

        Dim strSex As String

        If (Integer.Parse(cid.Substring(16, 1)) Mod 2) = 1 Then

            strSex = "男"

        Else

            strSex = "女"

        End If

        Return aCity(Integer.Parse(cid.Substring(0, 2))) & "," & cid.Substring(6, 4) & "-" & cid.Substring(10, 2) & "-" & cid.Substring(12, 2) & "," & strSex

End Function
http://www.Aspx1.Com
请帮忙宣传Aspx1 , Aspx1是ASP.NET学习者的家园 , 适宜长期居住.
 

回复:.NET中对18位身份证进行验证的方法

.NET 15位身份证升级到18位的算法     

VB函数 
    Public Shared Function per15To18(ByVal perIDSrc As String) As String

        Dim [iS] As Integer = 0

        '加权因子常数 

        Dim iW As Integer() = New Integer() {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}

        '校验码常数 

        Dim LastCode As String = "10X98765432"

        '新身份证号 

        Dim perIDNew As String

 

        perIDNew = perIDSrc.Substring(0, 6)

        perIDNew += "19"

        perIDNew += perIDSrc.Substring(6, 9)

        For i As Integer = 0 To 16

            '进行加权求和 

            [iS] += Integer.Parse(perIDNew.Substring(i, 1)) * iW(i)

        Next

 

        '取模运算,得到模值 

        Dim iY As Integer = [iS] Mod 11

        '从LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。 

        perIDNew += LastCode.Substring(iY, 1)

        Return perIDNew

    End Function

C#函数 
        public static string per15To18(string perIDSrc) 

        { 

              int iS = 0; 

              //加权因子常数 

              int[] iW=new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

              //校验码常数 

              string LastCode="10X98765432"; 

              //新身份证号 

              string perIDNew; 

              perIDNew=perIDSrc.Substring(0,6);

              perIDNew += "19"; 

              perIDNew += perIDSrc.Substring(6,9); 

              //进行加权求和 

              for( int i=0; i<17; i++) 

              { 

                  iS += int.Parse(perIDNew.Substring(i,1)) * iW

              } 

   

              //取模运算,得到模值 

              int iY = iS%11;

              //从LastCode中取得以模为索引号的值,加到身份证的最后一位,即为新身份证号。 

              perIDNew += LastCode.Substring(iY,1); 

              return perIDNew;

        }
http://www.Aspx1.Com
请帮忙宣传Aspx1 , Aspx1是ASP.NET学习者的家园 , 适宜长期居住.
 
1  /  1  页   1 跳转

版权所有 ASP.NET学习门户 2.0.1214   Sitemap  

返顶部