我想为我的小应用程序验证国民身份证号码。
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 验证这一点?我可以使用正则表达式来验证吗?
最佳答案
你可以在没有 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
}
关于c# - 身份证号码验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16351929/