Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identity cards number validation

Tags:

c#

validation

I want to validate National identity card number for my small application.

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

How can I validate this using visual studio 2010? can I use regular expression to validate this?

like image 368
Andare Avatar asked Sep 14 '25 10:09

Andare


1 Answers

You can do that without REGEX like:

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
}
like image 172
Habib Avatar answered Sep 16 '25 00:09

Habib