Java code for detailed verification of the ID number rules and names (kanji)

Source: Internet
Author: User

A regular expression for verifying Chinese characters

/** is the regular character of Chinese characters */private String Regexishanzi = "[\\u4e00-\\u9fa5]+"; * @param strthe string to match* @param regRegularpublic static Boolean matchregular (String str, string reg) {Pattern pattern = Pattern.compile (reg);  Matcher Matcher = Pattern.matcher (str); return matcher.matches (); }Second, verify the ID card number this thought simple to write a regular expression is OK,/** matches the number of digits of the ID number * /String Regxid = "([0-9]{17} ([0-9]| X)) | ([0-9]{15}) ";There are more precise rules matching the following, online learning, backup used. Respect for the fruits of others ' work, Original: Http://blog.csdn.net/suncaoyong/article/details/8647037import Java.text.simpledateformat;import Java.util.Date;
/** * Verify the identity card number, can parse the ID number of the various fields, and verify that the ID card number is valid; ID Number composition: 6-bit address code + 8-bit birthday + 3-bit sequential code + 1-bit check code * * @ClassName: Checkidcard * @Description: * @author Neng * @date 2013-1-4 11:06:09 * * /public class Checkid {
Private String Cardnumber; Full ID number private Boolean cachevalidateresult = null; The cache ID is valid because the validation validity is used frequently and the complex private Date cachebirthdate = null is calculated; Cache Birth date because the birth date is used frequently and the complex private final static String Birth_date_format = "YyyyMMdd" is calculated; The format of the birth date in the ID number is private final static date Minimal_birth_date = new Date ( -2209017600000l); Minimum date of birth of the identity card, January 1, 1900 private final static int new_card_number_length = 18; Private final static int old_card_number_length = 15; Private final static char[] Verify_code = {' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '}; The last checksum in the 18-digit ID is private final static int[] Verify_code_weight = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2}; 18-bit ID, the weight of each number in the generated check code
/** * @Description: false error, true correct * @return * @author: Liuzhao * @date: November 18, 2014 */
 public Boolean validate () {  if (null = = Cachevalidateresult) {   boolean result = true;    result = result && (null! = Cardnumber); The ID number cannot be empty    result = result && New_card_number_length = = Cardnumber.length (); The ID number is 18 (new certificate)    //The first 17 digits of the ID number must be Arabic numerals    for (int i = 0; result && i < New_card_number_len GTH-1; i++) {    Char ch = cardnumber.charat (i);    result = result && ch >= ' 0 ' && ch &lt = ' 9 ';   }   //ID number 18th digit checksum correct    result = result && (Calculateverifycode ( Cardnumber) = = Cardnumber.charat (new_card_number_length-1));   //The date of birth cannot be later than the current time and cannot be earlier than 1900     Try {    Date birthDate = this.getbirthdate ();    result = result && null! = birthdate;    result = result && Birthdate.before (New Date ());    result = result && Birthdate.after (MI Nimal_birth_date);   /**     * date of birth must be correct, such as the month range is [1,12], the date range is [1,31], but also need to check the case of leap years, Otsuki, Xiao Yue, Month and date match      */    String birthdaypart = This.getbirthdaypart ();    string Realbirthdaypart = This.createbirthdateparser (). Format (birthDate);    result = result && ( Birthdaypart.equals (Realbirthdaypart));   } catch (Exception e) {    result = false;   }& nbsp  cachevalidateresult = boolean.valueof (Result);   //full ID number of provincial and county District inspection rules  }  return cachevalidateresult; }
/** * If it is a 15-digit ID number, it is automatically converted to 18-bit * * @param cardnumber * @return */public Checkid (String cardnumber) {if (null! = Card   Number) {Cardnumber = Cardnumber.trim ();   if (old_card_number_length = = Cardnumber.length ()) {Cardnumber = Conterttonewcardnumber (Cardnumber); }} this.cardnumber = Cardnumber; }
Public String Getcardnumber () {return cardnumber;}
Public String Getaddresscode () {this.checkifvalid (); Return this.cardNumber.substring (0, 6); }
Public Date getbirthdate () {if (null = = This.cachebirthdate) {try {this.cachebirthdate = This.createbirthdatepars   ER (). Parse (This.getbirthdaypart ());   } catch (Exception e) {throw new RuntimeException ("Invalid date of birth of the identity card"); }} return new Date (This.cacheBirthDate.getTime ()); }
public Boolean Ismale () {return 1 = = This.getgendercode ();}
public Boolean isfemal () {return false = = This.ismale ();}
/** * Get ID number 17th, odd male, even female * * @return */private int getgendercode () {this.checkifvalid ();  Char Gendercode = this.cardNumber.charAt (new_card_number_length-2); Return (((int) (Gendercode-' 0 ') & 0x1); }
Private String Getbirthdaypart () {return this.cardNumber.substring (6, 14);}
Private SimpleDateFormat Createbirthdateparser () {return new SimpleDateFormat (Birth_date_format);}
private void Checkifvalid () {if (false = = This.validate ()) {throw new runtimeexception ("ID number is incorrect!")  "); } }
/** * Check code (18th digit): * * 17-bit digital body Code weighted summation Formula S = Sum (Ai * Wi), i = 0...16, first 17 digits of the sum of the right; Ai: Indicates the number of the ID number on position i location Wi: Indicates the weighted factor W at position I I:7 9 * 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2; Calculate modulo Y = mod (S, one) < get the corresponding check code via modulo y:0 1 2 3 4 5 6 7 8 9 10 Check code: 1 0 X 9 * 8 7 6 5 4 3 2 * * @param cardnumber * @re  Turn */private static char Calculateverifycode (charsequence cardnumber) {int sum = 0;   for (int i = 0; i < new_card_number_length-1; i++) {Char ch = cardnumber.charat (i);  Sum + = ((int) (CH-' 0 ')) * verify_code_weight[i]; } return verify_code[sum% 11]; }
/** * Convert 15-digit ID number to 18-digit ID number <br> * 15-digit ID number and 18-digit ID card number, the difference is:<br> * 1, 15-digit ID number, the "Year of birth" field is 2 bits, the conversion needs to fill in "19", indicating 20th century <br> * 2, 15-digit ID no last check code. 18-bit ID, the checksum is based on the first 17 bits generated * * @param cardnumber * @return * * * private static string Conterttonewcardnumber (String Oldcardn  umber) {StringBuilder buf = new StringBuilder (new_card_number_length);  Buf.append (oldcardnumber.substring (0, 6));  Buf.append ("19");  Buf.append (oldcardnumber.substring (6));  Buf.append (Checkid.calculateverifycode (BUF)); return buf.tostring (); }

Java code for detailed verification of the ID number rules and names (kanji)

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.