我想验证我的小型应用程序的国民身份证号码。
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
Run Code Online (Sandbox Code Playgroud)
如何使用 Visual Studio 2010 验证这一点?我可以使用正则表达式来验证这一点吗?
您可以在没有 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
}
Run Code Online (Sandbox Code Playgroud)