身份证号码验证

时间:2013-05-03 05:05:18

标签: c# validation

我想验证我的小型应用程序的国民身份证号码。

There are only 9 digits
there is a letter at the end 'x' or 'v' (both capital and simple letters)
3rd digit can not be equal to 4 or 9

如何使用visual studio 2010验证这一点?我可以使用正则表达式来验证这个吗?

7 个答案:

答案 0 :(得分:4)

你可以在没有REGEX的情况下做到这一点:

string str = "124456789X";
if ((str.Count(char.IsDigit) == 9) && // only 9 digits
    (str.EndsWith("X", StringComparison.OrdinalIgnoreCase)
     || str.EndsWith("V", StringComparison.OrdinalIgnoreCase)) && //a letter at the end 'x' or 'v'
    (str[2] != '4' && str[2] != '9')) //3rd digit can not be equal to 4 or 9
{
    //Valid

}
else
{
    //invalid
}

答案 1 :(得分:1)

你可以试试这个

 if ((ID.Count(char.IsDigit) == 9) && (ID.EndsWith("X") || ID.EndsWith("V")) && 
    (ID[2] != '4' && ID[2] != '9'))) 
                {
                    //Valid

                }
                else
                {
                    //invalid
                }

答案 2 :(得分:0)

我会引导你为此建立一个正则表达式但我实际上不会为你做这件事 从空白正则表达式开始:

-------------

你最后想要'X'或'V'

------(X or V)

前面两位数

(2 digits)--------(X or V)

非4位或9位

(2 digits)(digit not 4 or 9)-----(X or V)

6位数

(2 digits)(digit not 4 or 9)(6 digits)(X or V)

答案 3 :(得分:0)

斯里兰卡网卡验证 - javascript

旧网卡格式 - 例如:'641042757V'

/^[0-9]{9}[vVxX]$/

新的NIC格式 - 例如:'196410402757' (第8位必须为0)

/^[0-9]{7}[0][0-9]{4}$/

答案 4 :(得分:0)

**Sri Lankan NIC Validation**

$("input[name='number']").on('keydown', function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    //alert(key);
    if (!([8, 9, 13, 27, 46, 110, 190,17, 88, 89].indexOf(key) !== -1 ||
            (key == 65 && (e.ctrlKey || e.metaKey)) ||
            (key >= 35 && key <= 40) ||
            (key >= 48 && key <= 57 && !(e.shiftKey || e.altKey)) ||
            (key >= 96 && key <= 105)
        )) e.preventDefault();

});

答案 5 :(得分:0)

Sriankan NIC验证-JavaScript正则表达式(ECMAScripts)

旧版NIC格式-例如:“ 952521381V”

  1. 包含9位数字和1个字母

  2. 第三个数字不等于“ 4”和“ 9”

  3. 9位数字后,包含v或x

    ^ \ d {2}(?:[0-35-8] \ d \ d(?<!(?: 000 | 500 | 36 [7-9] | 3 [7-9] \ d | 86 [7-9] | 8 [7-9] \ d))))d {4}(?:[vVxX])$

新的NIC格式-例如:“ 199525201381”

  1. 包含9位数字

  2. 第5位数字不等于“ 4”和“ 9”

  3. 第8个数字等于“ 0”

  4. 只有数字不包含字母

    ^(?: 19 | 20)?\ d {2}(?:[0-35-8] \ d \ d(?<!(?: 000 | 500 | 36 [7-9] | 3 [7-9] \ d | 86 [7-9] | 8 [7-9] \ d))))[0] \ d?\ d {4} $

  • 为了获得更好的解释,请测试regex101.com中的正则表达式

https://regex101.com/

答案 6 :(得分:0)

最近有两种类型的NIC编号模式。新NIC仅具有12位数字组合,而旧NIC具有9位数字,具有X,V或x,v组合。因此,我们必须分别检查每个NIC模式。在这里,我为新的NIC验证和旧的NIC验证添加了单独的逻辑。

用于旧NIC验证

public static bool validateNICOld(String nic) {
    int length = nic.length();
    if ((length != 10)) {
        return false;
    } else {
        char lastChar = nic.charAt((length - 1));
        String lastCharacter = String.valueOf(lastChar);
        if ((lastCharacter.equalsIgnoreCase("v") || lastCharacter.equalsIgnoreCase("x"))) {
            String number = nic.substring(0, (length - 1));
            Log.d("NUmber", number);
            return !number.trim().matches("/^[0-9]{9}/");
        } else {
            for (int i = 0; (i
                    < (length - 2)); i++) {
                char currentChar = nic.charAt(i);
                if (((currentChar < '0') || ('9' < currentChar))) {
                    return false;
                }
            }
        }
    }
    return false;
}

用于新的NIC验证

 public static bool validateNICNew(String nic) {
    int length = nic.length();
    if ((length != 12)) {
        return false;
    } else {
        Log.d("NIC", nic);
        return !nic.trim().matches("/[0-9]{12}/");

    }
}
相关问题