Code
Private Bool Checkidcard ( String ID)
{
If (Id. Length = 18 )
{
Bool Check = Checkidcard18 (ID );
Return Check;
}
Else If (Id. Length = 15 )
{
Bool Check = Checkidcard15 (ID );
Return Check;
}
Else
{
Return False ;
}
}
Private Bool Checkidcard18 ( String ID)
{
Long N = 0 ;
If ( Long . Tryparse (Id. Remove ( 17 ), Out N) = False | N < Math. Pow ( 10 , 16 ) | Long . Tryparse (Id. Replace ( ' X ' , ' 0 ' ). Replace ( ' X ' , ' 0 ' ), Out N) = False )
{
Return False ; // Digit Verification
}
String Address = " 11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62X15x33x42X51X63x21x34X43x52x64x65x71x81x82x91 " ;
If (Address. indexof (Id. Remove ( 2 )) = - 1 )
{
Return False ; // Province Verification
}
String Birth = Id. substring ( 6 , 8 ). Insert ( 6 , " - " ). Insert ( 4 , " - " );
Datetime time = New Datetime ();
If (Datetime. tryparse (birth, Out Time) = False )
{
Return False ; // Birthday Verification
}
String [] Arrvarifycode = ( " 1, 0, x, 9, 8, 7, 6, 5, 4, 3, 2 " ). Split ( ' , ' );
String [] Wi = ( " , 2 " ). Split ( ' , ' );
Char [] Ai = Id. Remove ( 17 ). Tochararray ();
Int Sum = 0 ;
For ( Int I = 0 ; I < 17 ; I ++ )
{
Sum + = Int . Parse (WI) * Int . Parse (AI. tostring ());
}
Int Y = - 1 ;
Math. divrem (sum, 11 , Out Y );
If (Arrvarifycode [y] ! = Id. substring ( 17 , 1 ). Tolower ())
{
Return False ; // Verification code verification
}
Return True ; // Compliant with GB11643-1999 standards
}
Private Bool Checkidcard15 ( String ID)
{
Long N = 0 ;
If ( Long . Tryparse (ID, Out N) = False | N < Math. Pow ( 10 , 14 ))
{
Return False ; // Digit Verification
}
String Address = " 11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62X15x33x42X51X63x21x34X43x52x64x65x71x81x82x91 " ;
If (Address. indexof (Id. Remove ( 2 )) = - 1 )
{
Return False ; // Province Verification
}
String Birth = Id. substring ( 6 , 6 ). Insert ( 4 , " - " ). Insert ( 2 , " - " );
Datetime time = New Datetime ();
If (Datetime. tryparse (birth, Out Time) = False )
{
Return False ; // Birthday Verification
}
Return True ; // Meets 15-digit ID card standards
}