身份证号码验证函数

private bool CheckCardNumber(string number) 

{ 

    int[] iW = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 }; 

    int iSum = 0; 

    for (int i = 0; i < 17; i++) 

    { 

        int iVal = 0; 

        int.TryParse(number[i].ToString(), out iVal); 

 

        iSum += iVal * iW[i]; 

    } 

 

    int iCheck = iSum % 11; 

 

    string check = string.Empty; 

 

    switch (iCheck) 

    { 

        case 0: check = "1"; break; 

        case 1: check = "0"; break; 

        case 2: check = "x"; break; 

        case 3: check = "9"; break; 

        case 4: check = "8"; break; 

        case 5: check = "7"; break; 

        case 6: check = "6"; break; 

        case 7: check = "5"; break; 

        case 8: check = "4"; break; 

        case 9: check = "3"; break; 

        case 10: check = "2"; break; 

    } 

 

    string valid = number[17].ToString().ToLower(); 

 

    return valid == check; 

} 

你可能感兴趣的:(身份证)