Skip to content

Instantly share code, notes, and snippets.

@kentyeh
Last active February 5, 2021 03:53
Show Gist options
  • Save kentyeh/48b8f8989a18cf3893b577ef11af11f0 to your computer and use it in GitHub Desktop.
Save kentyeh/48b8f8989a18cf3893b577ef11af11f0 to your computer and use it in GitHub Desktop.
public static boolean isValidIDorRCNumber(String ID) {
if (ID == null || ID.trim().isEmpty() || !ID.matches("[A-Za-z][A-Da-d1289]\\d{8}")) {
return false;
}
String head = "ABCDEFGHJKLMNPQRSTUVXYWZIO";
char[] id = ID.toUpperCase().toCharArray();
int chksum = (head.indexOf(id[0]) + 10) / 10
+ (head.indexOf(id[0]) + 10) % 10 * 9 % 10;
chksum += (id[1] > '@' && id[1] < 'E') ? ((head.indexOf(id[1]) + 10) % 10) * 8 % 10
: (id[1] - 48) * 8 % 10;
for (int i = 2, j = 7; i < 9; i++, j--) {
chksum += ((id[i] - 48) * j % 10);
}
return (10 - (chksum % 10)) % 10 == (id[9] - 48);
}
function isValidIDorRCNumber(str){
if(!str || !/[A-Za-z][A-Da-d1289]\d{8}/.test(str))
return false;
/*第1碼照位置展開成2碼數字,第2碼若不是數字,則照位置展開取個位,其餘與一般身份證驗證規則同*/
var head="ABCDEFGHJKLMNPQRSTUVXYWZIO";
var id = str.toUpperCase().split("");
var chksum = Math.floor((head.indexOf(id[0])+10)/10)+
(head.indexOf(id[0])+10)%10*9%10;
chksum += isNaN(Number(id[1]))?((head.indexOf(id[1])+10)%10)*8%10:Number(id[1])*8%10;
for(var i=2,j=7;i<9;i++,j--)
chksum+=Number(id[i])*j%10;
return (10 - (chksum % 10)) % 10 == Number(id[9]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment