ID card format verification (including 15-bit conversion to 18-bit) C #

Source: Internet
Author: User
Tags tostring
Transformation

Using System;
Using System.Collections.Generic;
Using System.Text;

Namespace ConsoleApplication1
{
Class Program
{
static void Main (string[] args)
{
for (int i = 1; i < i++)
{
Console.Write ("{0}:", i);
for (int k = 1; k <= i; k++)
{
Console.Write ("{0}" + "", I * k);
}


Console.WriteLine ();
}
String str = "620102197811196218";
Checkcid t = new checkcid ();
String str1 = T.checkcidinfo (str);
Console.Write (STR1);
}

<summary>
19
</summary>
public class Idcard
{
WI =2 (n-1) (mod 11)
Int[] wi = new int[] {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
Verify digit
Int[] VI = new int[] {1, 0, ' X ', 9, 8, 7, 6, 5, 4, 3, 2};
Private int[] ai = new int[18];
Public Idcard ()
{
}
Verify
public string Verify (string idcard)
{
String strcard = "";
if (Idcard. Length = = 15)
{
Strcard = Uptoeighteen (Idcard);
}
if (Idcard. Length = = 18)
{
Strcard = Idcard;
}
String verify = Idcard. Substring (17, 18);
if (verify. Equals (Getverify (Idcard))) {
return true;
//}
return strcard;
}
Get verify
public string getverify (string eightcardid)
{
int remaining = 0;
if (Eightcardid. Length = = 18)
{
Eightcardid = Eightcardid. Substring (0, 17);
}
if (Eightcardid. Length = = 17)
{
int sum = 0;
for (int i = 0; i < i++)
{
String k = Eightcardid. Substring (i, 1);
Ai[i] = Int. Parse (k);
}
for (int i = 0; i < i++)
{
sum = sum + wi[i] * Ai[i];
}
remaining = sum% 11;
}
return remaining = = 2? "X": (Vi[remaining]). ToString ();
}
Update to 18
public string Uptoeighteen (string fifteencardid)
{
String eightcardid = Fifteencardid. Substring (0, 6);
Eightcardid = Eightcardid + "19";
Eightcardid = Eightcardid + fifteencardid. Substring (6, 9);
Eightcardid = Eightcardid + getverify (eightcardid);
return eightcardid;
}
}
<summary>
Detection
</summary>
public class Checkcid
{
public string Checkcidinfo (String cid)
{
string[] acity = new String[]{null,null,null,null,null,null,null,null,null,null,null, "Beijing", "Tianjin", "Hebei", "Shanxi", "Inner Mongolia", Null,null,null,null,null, "Liaoning", "Jilin", "Heilongjiang", Null,null,null,null,null,null,null, "Shanghai", "Jiangsu", "Zhejiang", "Ann Micro", "Fujian", "Jiangxi", "Shandong" , Null,null,null, "Henan", "Hubei", "Hunan", "Guangdong", "Guangxi", "Hainan", Null,null,null, "Chongqing", "Sichuan", "Guizhou", "Yunnan", "Tibet", Null,null,null,null,null , NULL, "Shaanxi", "Gansu", "Qinghai", "Ningxia", "Xinjiang", Null,null,null,null,null, "Taiwan", Null,null,null,null,null,null,null,null,null, "Hong Kong" , "Macao", Null,null,null,null,null,null,null,null, "abroad"};
Double isum=0;
String info= "";
System.Text.RegularExpressions.Regex RG = new System.Text.RegularExpressions.Regex (@ "^\d{17} (\D|X) $");
System.Text.RegularExpressions.Match MC = RG. Match (CID);
if (!MC. Success)
{
Return "";
}
CID = CID. ToLower ();
CID = CID. Replace ("X", "a");
if (acity[int. Parse (CID. Substring (0,2))]==null)
{
Return to "illegal area";
}
Try
{
DateTime.Parse (CID. Substring (6,4) + "-" +cid. Substring (10,2) + "-" +cid. Substring (12,2));
}
Catch
{
Return "Illegal birthday";
}
for (int i=17;i>=0;i--)
{
Isum + = (System.Math.Pow (2,i)%11) *int. Parse (Cid[17-i]. ToString (), System.Globalization.NumberStyles.HexNumber);

}
if (isum%11!=1)
Return ("Illegal card number");

Return (Acity[int. Parse (CID. Substring (0,2))]+ "," +cid. Substring (6,4) + "-" +cid. Substring (10,2) + "-" +cid. Substring (12,2) + "," + (int. Parse (CID. Substring (16,1))%2==1? " Male ":" female "));

}
}
}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.