C implements a method to verify that an ID card is valid

  • 2021-01-22 05:18:39
  • OfStack

This article illustrates how C# implements a method to verify that an ID card is valid. Share with you for your reference. The specific analysis is as follows:

This C# code mainly verifies whether the beginning of the ID card and the format and length of the ID card are correct, instead of strictly verifying according to the coding rules of the ID card


/// <summary>
///  Verify that your ID card is legitimate 
/// </summary>
/// <param name="idCard"> The ID card to be verified </param>    
public static bool IsIdCard(string idCard)
{
  // If it is empty, the validation is considered qualified 
  if (IsNullOrEmpty(idCard))
  {
 return true;
  }
  // Clear the whitespace in the string to validate 
  idCard = idCard.Trim();
  // Pattern string 
  StringBuilder pattern = new StringBuilder();
  pattern.Append(@"^(11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|");
  pattern.Append(@"50|51|52|53|54|61|62|63|64|65|71|81|82|91)");
  pattern.Append(@"(\d{13}|\d{15}[\dx])$");
  // validation 
  return RegexHelper.IsMatch(idCard, pattern.ToString());
}

I hope this article is helpful to your C# program design.


Related articles: