Last active
August 29, 2015 13:56
-
-
Save jerry74/8838235 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 身份證字號與居留證(統一證)編號檢核 | |
| * | |
| * @param id | |
| * @return | |
| */ | |
| public static boolean isValidIDorRCNumber(String id) | |
| { | |
| id = id.toUpperCase();// 轉換大寫 | |
| char[] strArr = id.toCharArray();// 字串轉成char陣列 | |
| int verifyNum = 0; | |
| /* 檢查身份證字號 */ | |
| if (id.matches("[A-Z]{1}[1-2]{1}[0-9]{8}")) | |
| { | |
| // 第一碼 | |
| verifyNum +=(Character.getNumericValue(strArr[0])/10)%10; | |
| verifyNum +=Character.getNumericValue(strArr[0])%10; | |
| // 第二~九碼 | |
| for (int i = 1, j = 8; i < 9; i++, j--) | |
| { | |
| verifyNum += Character.digit(strArr[i], 10) * j; | |
| } | |
| // 檢查碼 | |
| verifyNum = (10 - (verifyNum % 10)) % 10; | |
| if (verifyNum == Character.digit(strArr[9], 10)) | |
| return true; | |
| } | |
| /* 檢查統一證(居留證)編號 */ | |
| verifyNum = 0; | |
| if (id.matches("[A-Z]{1}[A-D]{1}[0-9]{8}")) | |
| { | |
| // 第一碼 | |
| verifyNum +=(Character.getNumericValue(strArr[0])/10)%10; | |
| verifyNum +=Character.getNumericValue(strArr[0])%10; | |
| // 第二碼 | |
| verifyNum+=( ((Character.getNumericValue(strArr[1]))% 10)*8)% 10; | |
| // 第三~九碼 | |
| for (int i = 2, j = 7; i < 10; i++, j--) | |
| { | |
| verifyNum += (Character.digit(strArr[i],10 )* j)%10; | |
| } | |
| // 檢查碼 | |
| verifyNum = (10 - (verifyNum % 10)) % 10; | |
| if (verifyNum == Character.digit(strArr[9], 10)) | |
| return true; | |
| } | |
| return false; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment