Skip to content

IDCard EN

皮卡超人 edited this page Dec 3, 2025 · 1 revision

ID Card Validation and Information Extraction

Language: 中文 (Chinese) | English

WodToolKit provides complete Chinese ID card number validation and information extraction functionality, including ID card number legality verification, address information extraction, and gender identification.

Overview

The IDCard class provides the following static methods:

  • IsIdCard(string idCard) - Validates whether an ID card number is legal
  • GetCardAddress(string card) - Extracts address information (province, city, district/county) from an ID card number
  • GetGender(string card) - Extracts gender information from an ID card number

Basic Usage

Validate ID Card Number

using WodToolkit.src.Common;

string idCard = "110101199001011234";
bool isValid = IDCard.IsIdCard(idCard);

if (isValid)
{
    Console.WriteLine("ID card number is valid");
}
else
{
    Console.WriteLine("ID card number is invalid");
}

Get Address Information

The GetCardAddress method returns a List<string> containing up to 3 elements:

  • 1st element: Province name
  • 2nd element: City name
  • 3rd element: District/County name
string idCard = "110101199001011234";
List<string> addressList = IDCard.GetCardAddress(idCard);

if (addressList.Count >= 2)
{
    Console.WriteLine($"Province: {addressList[0]}");
    Console.WriteLine($"City: {addressList[1]}");
    
    if (addressList.Count >= 3)
    {
        Console.WriteLine($"District: {addressList[2]}");
    }
}
else if (addressList.Count == 1)
{
    // If detailed address cannot be obtained, at least return province information
    Console.WriteLine($"Province: {addressList[0]}");
}

Address Extraction Logic:

  1. First attempts to precisely match province, city, and district/county information from the built-in regional database
  2. If at least 2 levels (province + city) are matched, returns the complete address list directly
  3. If no match or only 1 level is found, returns the province name from the province mapping table based on the first two digits of the ID card

Get Gender Information

The second-to-last digit (17th position) of a Chinese second-generation ID card is used to indicate gender:

  • Odd number = Male
  • Even number = Female
string idCard = "110101199001011234";
string gender = IDCard.GetGender(idCard);

Console.WriteLine($"Gender: {gender}"); // Output: "男" (Male) or "女" (Female)

Note: If the ID card number is invalid or has incorrect length, the GetGender method returns an empty string.

Complete Example

using WodToolkit.src.Common;
using System.Collections.Generic;

string idCard = "110101199001011234";

// 1. Validate ID card number
if (IDCard.IsIdCard(idCard))
{
    Console.WriteLine("✓ ID card number validation passed");
    
    // 2. Get address information
    List<string> address = IDCard.GetCardAddress(idCard);
    if (address.Count > 0)
    {
        Console.WriteLine($"Address: {string.Join(" ", address)}");
    }
    
    // 3. Get gender
    string gender = IDCard.GetGender(idCard);
    if (!string.IsNullOrEmpty(gender))
    {
        Console.WriteLine($"Gender: {gender}");
    }
}
else
{
    Console.WriteLine("✗ ID card number is invalid");
}

API Reference

IsIdCard

Validates whether a 18-digit ID card number is legal.

Method Signature:

public static bool IsIdCard(string idCard)

Parameters:

  • idCard (string): 18-digit ID card number

Return Value:

  • bool: true indicates the ID card number is legal, false indicates it is illegal

Validation Rules:

  • ID card number must be 18 digits
  • First 17 digits must be numbers
  • Last digit can be a number or letter X (case-insensitive)
  • Validates the last check digit using weighted factor and check code algorithm

GetCardAddress

Extracts address information from an ID card number.

Method Signature:

public static List<string> GetCardAddress(string card)

Parameters:

  • card (string): ID card number (at least the first 6 digits of area code required)

Return Value:

  • List<string>: Address information list, containing up to 3 elements (province, city, district/county)

Notes:

  • Returns empty list if ID card number length is less than 6 digits
  • If detailed address cannot be matched from the regional database, returns province name based on the first two province code digits
  • The number of elements in the returned list may be 0, 1, 2, or 3

GetGender

Extracts gender information from an ID card number.

Method Signature:

public static string GetGender(string card)

Parameters:

  • card (string): 18-digit ID card number

Return Value:

  • string: "男" (Male) or "女" (Female), returns empty string if ID card is invalid

Notes:

  • Returns empty string if ID card number length is not 18 digits
  • Returns empty string if ID card number is illegal (can be validated using IsIdCard)
  • Determines gender based on the parity of the 17th digit

Notes

  1. ID Card Number Format: All methods require 18-digit ID card numbers (Chinese second-generation ID card standard)

  2. Address Data: Address information comes from the built-in regional database, covering all provinces, cities, and districts/counties nationwide

  3. Performance: The IsIdCard method executes very quickly and is suitable for large-scale data validation scenarios

  4. Error Handling: All methods include basic parameter validation, invalid inputs return safe default values (false, empty list, or empty string)

  5. Thread Safety: All methods are static and thread-safe, can be used in multi-threaded environments

Use Cases

  • ID card number validation during user registration
  • Form data validation
  • ID card information extraction and display
  • Data cleaning and validation
  • Batch ID card number validation

Related Links

Clone this wiki locally