正则表达式,多么强大的工具类,大家都在用它,大家会用它判断身份证,我也用到了它,但是我发现对于香港身份证是验证不了的,我们大陆最新的身份证是18位的,香港身份证可不是的,这些规则我大致总结一下如下:
好了,上代码,OC代码如下,写的有点臃肿,后期优化
//验证香港身份证
[GlobalTool sharedGlobalTool] 是自定期的全局工具类
+ (BOOL)isCorrect:(NSString *)HKID
{
if (!HKID||HKID.length==0) {
return NO;
}
if ([HKID containsString:@"("])
{
HKID = [HKID stringByReplacingOccurrencesOfString:@"(" withString:@"("] ;
}
if ([HKID containsString:@")"])
{
HKID = [HKID stringByReplacingOccurrencesOfString:@")" withString:@")"] ;
}
if(HKID.length==10)
{
HKID = [NSString stringWithFormat:@" %@",HKID].uppercaseString;
}
if (HKID.length != 11) {
return NO ;
}
NSInteger value = 0;
char card1 = [HKID characterAtIndex:0];
if ((card1 !=' ')&& !(card1>='A'&&card1<='Z'))
{
return NO ;
}
char card2 = [HKID characterAtIndex:1];
if (!(card2>='A'&&card2<='Z'))
{
return NO;
}
char card3 = [HKID characterAtIndex:2];
char card4 = [HKID characterAtIndex:3];
char card5 = [HKID characterAtIndex:4];
char card6 = [HKID characterAtIndex:5];
char card7 = [HKID characterAtIndex:6];
char card8 = [HKID characterAtIndex:7];
if (!(card3>='0'&&card3<='9')||!(card4>='0'&&card4<='9')||!(card5>='0'&&card5<='9')||!(card6>='0'&&card6<='9')||!(card7>='0'&&card7<='9')|| !(card8>='0'&&card8<='9'))
{
return NO;
}
char card9 = [HKID characterAtIndex:8];
if (card9 != '(') {
return NO;
}
char card10 = [HKID characterAtIndex:9];
if (card10 != 'A' && card10 != 'a' && !(card10>='0'&&card10<='9')) {
return NO;
}
char card11 = [HKID characterAtIndex:10];
if (card11 != ')') {
return NO;
}
value = [[GlobalTool sharedGlobalTool]num:card1] * 9 + [[GlobalTool sharedGlobalTool]num:card2]* 8 + [[GlobalTool sharedGlobalTool]num:card3] * 7 +[[GlobalTool sharedGlobalTool]num:card4] *6 + [[GlobalTool sharedGlobalTool]num:card5] * 5 + [[GlobalTool sharedGlobalTool]num:card6] * 4 +[[GlobalTool sharedGlobalTool]num:card7]*3 + [[GlobalTool sharedGlobalTool]num:card8] *2 + [[GlobalTool sharedGlobalTool]num:card10];
if (value%11==0) {
return YES;
}
return NO;
}
- (int)num:(char)idCardX
{
if (idCardX == ' ') {
return 58;
}
else if (idCardX == 'A' || idCardX =='a') {
return 10;
}
else if (idCardX == 'B' || idCardX == 'b') {
return 11;
}
else if (idCardX == 'C' || idCardX == 'c') {
return 12;
}
else if (idCardX == 'D' || idCardX == 'd') {
return 13;
}
else if (idCardX == 'E' || idCardX == 'e') {
return 14;
}
else if (idCardX == 'F' || idCardX == 'f') {
return 15;
}
else if (idCardX == 'G' || idCardX == 'g') {
return 16;
}
else if (idCardX == 'H' || idCardX == 'h') {
return 17;
}
else if (idCardX == 'I' || idCardX == 'i') {
return 18;
}
else if (idCardX == 'J' || idCardX == 'j') {
return 19;
}
else if (idCardX == 'K' || idCardX == 'k') {
return 20;
}
else if (idCardX == 'L' || idCardX == 'l') {
return 21;
}
else if (idCardX == 'M' || idCardX == 'm') {
return 22;
}
else if (idCardX == 'N' || idCardX == 'n') {
return 23;
}
else if (idCardX == 'O' || idCardX == 'o') {
return 24;
}
else if (idCardX == 'P' || idCardX == 'p') {
return 25;
}
else if (idCardX == 'Q' || idCardX == 'q') {
return 26;
}
else if (idCardX == 'R'|| idCardX == 'r') {
return 27;
}
else if (idCardX == 'S' || idCardX == 's') {
return 28;
}
else if (idCardX == 'T' || idCardX == 't') {
return 29;
}
else if (idCardX == 'U' || idCardX == 'u') {
return 30;
}
else if (idCardX == 'V' || idCardX == 'v') {
return 31;
}
else if (idCardX == 'W' || idCardX == 'w') {
return 32;
}
else if (idCardX == 'X' || idCardX == 'x') {
return 33;
}
else if (idCardX == 'Y' || idCardX == 'y') {
return 34;
}
else if (idCardX == 'Z' || idCardX == 'z') {
return 35;
}
else if (idCardX == '0') {
return 0;
}
else if (idCardX == '1') {
return 1;
}
else if (idCardX == '2') {
return 2;
}
else if (idCardX == '3') {
return 3;
}
else if (idCardX == '4') {
return 4;
}
else if (idCardX == '5') {
return 5;
}
else if (idCardX == '6') {
return 6;
}
else if (idCardX == '7') {
return 7;
}
else if (idCardX == '8') {
return 8;
}
else if (idCardX == '9') {
return 9;
}
return 0;
下面是JS代码
//验证是否为香港身份证号码
function HKIdCardValidate_Common(idCard) {
if (idCard.length == 10) {
idCard = " " + idCard;
}
if (idCard.length != 11) {
return false;
}
idCard = idCard.toLocaleLowerCase(); //小写
//验证第一位数
var regExp1 = /[a-z]$/;
var idcard1 = idCard.substring(0, 1);
if (idcard1 != " " && !regExp1.test(idcard1)) {
return false;
}
//验证第二位数
var idcard2 = idCard.substring(1, 2);
if (!regExp1.test(idcard2)) {
return false;
}
//验证第三到第八位数
var regExp3 = /^[0-9]*$/;
var idcard3 = idCard.substring(2, 3);
var idcard4 = idCard.substring(3, 4);
var idcard5 = idCard.substring(4, 5);
var idcard6 = idCard.substring(5, 6);
var idcard7 = idCard.substring(6, 7);
var idcard8 = idCard.substring(7, 8);
if (!regExp3.test(idcard3) || !regExp3.test(idcard4) || !regExp3.test(idcard5) || !regExp3.test(idcard6) || !regExp3.test(idcard7) || !regExp3.test(idcard8)) {
return false;
}
//验证第九位数
var idcard9 = idCard.substring(8, 9);
if (idcard9 != "(" && idcard9 != "(") {
return false;
}
//验证第十位数
var idcard10 = idCard.substring(9, 10);
if (idcard10 != "A" && idcard10 != "a" && !regExp3.test(idcard10)) {
return false;
}
//验证第十一位数
var idcard11 = idCard.substring(10, 11);
if (idcard11 != ")" && idcard11 != ")") {
return false;
}
//验证身份证是否符合要求
var numidcard = 0;
numidcard = parseInt(HKIdCardXYZByIdCard(idcard1)) * 9 + parseInt(HKIdCardXYZByIdCard(idcard2)) * 8
+ parseInt(idcard3) * 7 + parseInt(idcard4) * 6 + parseInt(idcard5) * 5 + parseInt(idcard6) * 4
+ parseInt(idcard7) * 3 + parseInt(idcard8) * 2;
if (idcard10 == "A" || idcard10 == "a") {
numidcard = numidcard + parseInt(HKIdCardXYZByIdCard(idcard10));
}
else {
numidcard = numidcard + parseInt(idcard10);
}
if (numidcard % 11 != 0) {
return false;
}
return true;
}
function HKIdCardXYZByIdCard(idCardX) {
if (idCardX == " ") {
return 58;
}
if (idCardX == "A" || idCardX == "a") {
return 10;
}
if (idCardX == "B" || idCardX == "b") {
return 11;
}
if (idCardX == "C" || idCardX == "c") {
return 12;
}
if (idCardX == "D" || idCardX == "d") {
return 13;
}
if (idCardX == "E" || idCardX == "e") {
return 14;
}
if (idCardX == "F" || idCardX == "f") {
return 15;
}
if (idCardX == "G" || idCardX == "g") {
return 16;
}
if (idCardX == "H" || idCardX == "h") {
return 17;
}
if (idCardX == "I" || idCardX == "i") {
return 18;
}
if (idCardX == "J" || idCardX == "j") {
return 19;
}
if (idCardX == "K" || idCardX == "k") {
return 20;
}
if (idCardX == "L" || idCardX == "l") {
return 21;
}
if (idCardX == "M" || idCardX == "m") {
return 22;
}
if (idCardX == "N" || idCardX == "n") {
return 23;
}
if (idCardX == "O" || idCardX == "o") {
return 24;
}
if (idCardX == "P" || idCardX == "p") {
return 25;
}
if (idCardX == "Q" || idCardX == "q") {
return 26;
}
if (idCardX == "R" || idCardX == "r") {
return 27;
}
if (idCardX == "S" || idCardX == "s") {
return 28;
}
if (idCardX == "T" || idCardX == "t") {
return 29;
}
if (idCardX == "U" || idCardX == "u") {
return 30;
}
if (idCardX == "V" || idCardX == "v") {
return 31;
}
if (idCardX == "W" || idCardX == "w") {
return 32;
}
if (idCardX == "X" || idCardX == "x") {
return 33;
}
if (idCardX == "Y" || idCardX == "y") {
return 34;
}
if (idCardX == "Z" || idCardX == "z") {
return 35;
}
return 0;
}
如果大家公司的业务有用到香港身份证的验证,欢迎大家的使用。