C # verify the ID card number

Source: Internet
Author: User

 

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
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.