. NET verifies that a social security number conforms to a rule

Source: Internet
Author: User

Verify that the ID number is in accordance with the rules, more practical, the wrong rate of my own test for 0, very easy to use, welcome to the great God to add.

protected bool Checkidcard (string idcard) {//verify 18-bit or 17-bit XL lowercase x regex rg = new Regex (@ "^\d{17} ( \d|            X|X) $ "); Match MC = RG.            Match (Idcard);            string r = ""; if (!MC.            Success) return false;  Verify that the first two digits meet the region code string[] area = {"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64", "65", "71", "81", "            82 "," 91 "};            BOOL B = false; foreach (String A in area) {if (Idcard.                    Substring (0, 2) = = a) {b = true;                Break            }} if (b = = false) return false; Verify the Birth date string birthday = Idcard. Substring (6, 4) + "/" + Idcard. Substring (2) + "/" + Idcard.            Substring (12, 2); try {Convert.todAtetime (birthday);            } catch {return false; }//Verify that the last check digit conforms to the rule int i = (int. Parse (Idcard. Substring (0, 1)) * 7) + (int. Parse (Idcard. Substring (1, 1)) * 9) + (int. Parse (Idcard. Substring (2, 1)) * + (int. Parse (Idcard. Substring (3, 1)) * 5) + (int. Parse (Idcard. Substring (4, 1)) * 8) + (int. Parse (Idcard. Substring (5, 1)) * 4) + (int. Parse (Idcard. Substring (6, 1)) * 2) + (int. Parse (Idcard. Substring (7, 1)) * 1) + (int. Parse (Idcard. Substring (8, 1)) * 6) + (int. Parse (Idcard. Substring (9, 1)) * 3) + (int. Parse (Idcard. Substring (ten, 1)) * 7) + (int. Parse (Idcard. Substring (one, 1)) * 9) + (int. Parse (Idcard. Substring (1)) + (int. Parse (Idcard. Substring (1)) * 5) + (int. Parse (Idcard. Substring (14, 1)) * 8) + (int. Parse (Idcard. Substring (1)) * 4) + (int. Parse (Idcard.            Substring (16, 1)) * 2);            i = i-i/11 * 11;                switch (i) {case 0:r = "1"; Case 1:r = "0";                Break Case 2:r = "11";                Break Case 3:r = "9";                Break Case 4:r = "8";                Break Case 5:r = "7";                Break Case 6:r = "6";                Break Case 7:r = "5";                Break Case 8:r = "4";                Break Case 9:r = "3";                Break Case 10:r = "2";            Break            } if (r = = "One") R = "X"; if (r = = Idcard. ToLower ().            Substring (1)) return true;        else return false; }


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

. NET verifies that a social security number conforms to a rule

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.