SoFunction
Updated on 2025-03-07

C# method to implement ID number verification

/// <summary> 
/// Verify ID number category
/// </summary> 
public class IDCardValidation 

        /// <summary> 
/// Verify the rationality of the ID card
        /// </summary> 
        /// <param name="Id"></param> 
        /// <returns></returns> 
        public bool CheckIDCard(string idNumber) 
        { 
            if ( == 18) 
            { 
                bool check = CheckIDCard18(idNumber); 
                return check; 
            } 
            else if ( == 15) 
            { 
                bool check = CheckIDCard15(idNumber); 
                return check; 
            } 
            else 
            { 
                return false; 
            } 
        } 
 
 
        /// <summary> 
/// 18-digit ID number verification
        /// </summary> 
        private bool CheckIDCard18(string idNumber) 
        { 
            long n = 0; 
            if (((17), out n) == false  
                || n < (10, 16) || (('x', '0').Replace('X', '0'), out n) == false) 
            { 
return false;//Digital verification
            } 
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; 
            if (((2)) == -1) 
            { 
return false;//Provincial verification
            } 
            string birth = (6, 8).Insert(6, "-").Insert(4, "-"); 
            DateTime time = new DateTime(); 
            if ((birth, out time) == false) 
            { 
return false;//Birthday verification
            } 
            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 = (17).ToCharArray(); 
            int sum = 0; 
            for (int i = 0; i < 17; i++) 
            { 
                sum += (Wi[i]) * (Ai[i].ToString()); 
            } 
            int y = -1; 
            (sum, 11, out y); 
            if (arrVarifyCode[y] != (17, 1).ToLower()) 
            { 
return false;//Check code verification
            } 
return true;//Complied with GB11643-1999 standard
        } 
 
 
        /// <summary> 
/// 16-digit ID number verification
        /// </summary> 
        private bool CheckIDCard15(string idNumber) 
        { 
            long n = 0; 
            if ((idNumber, out n) == false || n < (10, 14)) 
            { 
return false;//Digital verification
            } 
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91"; 
            if (((2)) == -1) 
            { 
return false;//Provincial verification
            } 
            string birth = (6, 6).Insert(4, "-").Insert(2, "-"); 
            DateTime time = new DateTime(); 
            if ((birth, out time) == false) 
            { 
return false;//Birthday verification
            } 
            return true; 
        }  
}