c# - 身份证号码验证

标签 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 验证这一点?我可以使用正则表达式来验证吗?

最佳答案

你可以在没有 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/

相关文章:

c# - 如何在 WPF 中使用 ContextMenu UserControl?

c# - 我可以让 ProcessBytes 返回准确的数量吗?

java - 查找重复的表达式/参数

c - isalpha() 返回浮点/整数输入的非零值

c# - 如何在没有 JavaScript 的情况下验证表单?

java - Spring MVC + Hibernate validator + Velocity

c# - 即使虚拟目录允许匿名访问,Web 服务也会出现 "HTTP status 401: Access Denied"错误

c# - 将默认的 Roslyn 项目 dll 版本从 42.42.42.42 更改为 2.0.0.0

c# - 在 C# 中将\u0040 转换为 @

java - xjc错误: "Two declarations cause a collision" on valid . xsd