Code-Validator: verify ID number

ylbtech-Code-Validator: verify ID number

 

1. Back to top
1、
using System;

namespace Sp.Common
{
    public  class ValidatorHelper
    {
        ///  <summary>   
        /// Address corresponding to the number on the ID card  
         ///  </ summary>   
        // enum IDAddress  
         // {  
         //     Beijing = 11, Tianjin = 12, Hebei = 13, Shanxi = 14, Inner Mongolia = 15, Liaoning = 21, Jilin = 22, Heilongjiang = 23, Shanghai = 31, Jiangsu = 32, Zhejiang = 33,  
         //     Anhui = 34, Fujian = 35, Jiangxi = 36, Shandong = 37, Henan = 41, Hubei = 42, Hunan = 43, Guangdong = 44, Guangxi = 45, Hainan = 46, Chongqing = 50, Sichuan = 51,  
         //     Guizhou = 52, Yunnan = 53, Tibet = 54, Shaanxi = 61, Gansu = 62, Qinghai = 63, Ningxia = 64, Xinjiang = 65, Taiwan = 71, Hong Kong = 81, Macau = 82, abroad = 91  
         // }  

        ///  <summary>   
        /// Verify a generation ID card number (15 digits)  
         /// [15 digits long; match the corresponding province address; birthday can be correctly matched]  
         ///  </ summary>   
        ///  <param name = "input"> String to be verified </ param>   
        ///  <returns> Does it match </ returns>   
        public  static  bool IsIDCard15 ( string input)
        {
            // Verify whether it can be converted to a 15-bit integer   
            long l = 0 ;
             if (! Long .TryParse (input, out l) || l.ToString (). Length! = 15 )
            {
                return false;
            }
            // Verify that provinces match  
             // 1 to 6 digits are area codes, of which 1, 2 digits are codes of provincial governments, 3 and 4 digits are codes of local and municipal governments, and 5 and 6 digits are County and district government codes.  
            string address = " 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, " ;
             if (! Address.Contains (input.Remove ( 2 ) + " , " ))
            {
                return false;
            }
            // Verify that the birthday matches   
            string birthdate = input.Substring ( 6 , 6 ) .Insert ( 4 , " / " ) .Insert ( 2 , " / " );
            DateTime dt;
            if (!DateTime.TryParse(birthdate, out dt))
            {
                return false;
            }
            return true;
        }

        ///  <summary>   
        /// Verify the second generation ID card number (18 digits, GB11643-1999 standard)  
         /// [The length is 18 digits; the first 17 digits are numbers, and the last digit (check code) can be Case x; match the corresponding province address; birthday can match correctly; check code can match correctly]  
         ///  </ summary>   
        ///  <param name = "input"> string to be verified </ param>   
        // /  <returns> Does it match </ returns>   
        public  static  bool IsIDCard18 ( string input)
        {
            // Verify that it can be converted to the correct integer   
            long l = 0 ;
             if (! Long .TryParse (input.Remove ( 17 ), out l) || l.ToString (). Length! = 17 ||! Long .TryParse (input.Replace ( ' x ' , ' 0 ' ) .Replace ( ' X ' , ' 0 ' ), out l))
            {
                return false;
            }
            // Verify that provinces match  
             // 1 to 6 digits are area codes, of which 1, 2 digits are codes of provincial governments, 3 and 4 digits are codes of local and municipal governments, and 5 and 6 digits are County and district government codes.  
            string address = " 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, " ;
             if (! Address.Contains (input.Remove ( 2 ) + " , " ))
            {
                return false;
            }
            // Verify that the birthday matches   
            string birthdate = input.Substring ( 6 , 8 ) .Insert ( 6 , " / " ) .Insert ( 4 , " / " );
            DateTime dt;
            if (!DateTime.TryParse(birthdate, out dt))
            {
                return false;
            }
            // Verification code verification  
             // Verification code:  
             // (1) Weighted sum formula of 17-digit body code   
             // S = Sum (Ai * Wi), i = 0, ..., 16, first The sum of the weights of the first 17 digits   
             // Ai: indicates the digital value of the ID number at position i   
             // Wi: indicates the weighting factor at position i   
             // Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2   
             // (2) Calculate the modulus   
             // Y = mod (S, 11)   
             // (3) Get the corresponding check code through the modulus   
             // Y: 0 1 2 3 4 5 6 7 8 9 10   
             // Check code: 1 0 X 9 8 7 6 5 4 3 2    
            string [] arrVarifyCode = ( " 1,0, x, 9,8,7,6,5,4,3,2 ").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = input.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != input.Substring(17, 1).ToLower())
            {
                return false;
            }
            return true;
        }

        ///  <summary>   
        /// Verify the ID number (do not distinguish between the first and second generation ID numbers)  
         ///  </ summary>   
        ///  <param name = "input"> the string to be verified </ param>   
        ///  <returns> Does it match </ returns>   
        public  static  bool IsIDCard ( string input)
        {
            if (input.Length == 18)
                return IsIDCard18(input);
            else if (input.Length == 15)
                return IsIDCard15(input);
            else
                return false;
        }

    }
}
2、
2. Back to top
 
3. Back to top
 
4. Back to top
 
5. Back to top
 
 
6. Back to top
 
warn Author: ylbtech
Source: http://ylbtech.cnblogs.com/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise Reserves the right to pursue legal responsibility.

Guess you like

Origin www.cnblogs.com/storebook/p/12685759.html