SoFunction
Updated on 2025-03-07

C# function to verify ID card

This C# code mainly verifies whether the beginning of the ID card and the format and length of the ID card are correct, and does not strictly verify according to the encoding rules of the ID card

/// <summary>
    /// Verify whether the ID card is legal    /// </summary>
    /// <param name="idCard">ID card to be verified</param>    public static bool IsIdCard(string idCard)
    {
      //If it is empty, the verification is considered qualified      if (IsNullOrEmpty(idCard))
      {
        return true;
      }
 
      //Clear the spaces in the string to be validated      idCard = ();
 
      //Mode string      StringBuilder pattern = new StringBuilder();
      (@"^(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)");
      (@"(\d{13}|\d{15}[\dx])$");
 
      //verify      return (idCard, ());
    }

More complex verification methods:

private string CheckCidInfo(string cid)
 {
  string[] aCity = new string[]{null,null,null,null,null,null,null,null,null,null,null,"Beijing","Tianjin","Hebei","Shanxi","Inner *",null,null,null,null,null,"Liaoning","Jilin","Heilongjiang",null,null,null,null,null,null,null,"Shanghai","Jiangsu","Zhejiang","Anwei","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","*",null,null,null,null,null,"*",null,null,null,null,null,null,null,null,null,"Hongkong","Macao",null,null,null,null,null,null,null,null,"foreign"};
  double iSum=0;
  string info="";
   rg = new (@"^\d{17}(\d|x)$");
   mc = (cid);
  if(!)
  {
  return "";
  }  
  cid = ();
  cid = ("x","a");
  if(aCity[((0,2))]==null)
  {
  return "Illegal Area";
  }
  try
  {
  ((6,4)+"-"+(10,2)+"-"+(12,2));
  }
  catch
  {
  return "Illegal Birthday";
  }
  for(int i=17;i&gt;=0;i--)
  {  
  iSum +=((2,i)%11)*(cid[17-i].ToString(),);

  }
  if(iSum%11!=1)
  return("Illegal Certificate Number");
  
  return(aCity[((0,2))]+","+(6,4)+"-"+(10,2)+"-"+(12,2)+","+(((16,1))%2==1&amp;iexcl;"male":"female"));
 
 }

Call test:
(CheckCidInfo("340524198002300019"));
(CheckCidInfo("34052419800101001x"));

Method 3:

/// &lt;summary&gt;
/// ID card verification/// &lt;/summary&gt;
/// <param name="Id">ID number</param>/// &lt;returns&gt;&lt;/returns&gt;
public bool CheckIDCard(string Id)
{
  if ( == 18)
  {
     bool check = CheckIDCard18(Id);
     return check;
   }
   else if ( == 15)
   {
     bool check = CheckIDCard15(Id);
     return check;
   }
   else
   {
     return false;
   }
 }
 /// &lt;summary&gt;
 /// 18-digit ID card verification /// &lt;/summary&gt;
 /// <param name="Id">ID number</param> /// &lt;returns&gt;&lt;/returns&gt;
 PRivate bool CheckIDCard18(string Id)
 {
   long n = 0;
   if (((17), out n) == false || n &lt; (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 &lt; 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 }
 /// &lt;summary&gt;
 /// 15-digit ID card verification /// &lt;/summary&gt;
 /// <param name="Id">ID number</param> /// &lt;returns&gt;&lt;/returns&gt;
 private bool CheckIDCard15(string Id)
 {
   long n = 0;
   if ((Id, out n) == false || n &lt; (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;//Complied with the 15-digit ID card standard }

The above is the entire content of this article, I hope you like it.