欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript驗證香港身份證的格式或真實性

 更新時間:2017年02月07日 11:40:59   作者:尖峰時刻  
本文分享了利用javascript驗證香港身份證的格式或真實性的代碼,具有很好的參考價值,下面跟著小編一起來看下吧

話不多說,請看代碼

function IsHKID(str) {
 var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 // basic check length
 if (str.length < 8)
 return false;
 // handling bracket
 if (str.charAt(str.length-3) == '(' && str.charAt(str.length-1) == ')')
 str = str.substring(0, str.length - 3) + str.charAt(str.length -2);
 // convert to upper case
 str = str.toUpperCase();
 // regular expression to check pattern and split
 var hkidPat = /^([A-Z]{1,2})([0-9]{6})([A0-9])$/;
 var matchArray = str.match(hkidPat);
 // not match, return false
 if (matchArray == null)
 return false;
 // the character part, numeric part and check digit part
 var charPart = matchArray[1];
 var numPart = matchArray[2];
 var checkDigit = matchArray[3];
 // calculate the checksum for character part
 var checkSum = 0;
 if (charPart.length == 2) {
 checkSum += 9 * (10 + strValidChars.indexOf(charPart.charAt(0)));
 checkSum += 8 * (10 + strValidChars.indexOf(charPart.charAt(1)));
 } else {
 checkSum += 9 * 36;
 checkSum += 8 * (10 + strValidChars.indexOf(charPart));
 }
 // calculate the checksum for numeric part
 for (var i = 0, j = 7; i < numPart.length; i++, j--)
 checkSum += j * numPart.charAt(i);
 // verify the check digit
 var remaining = checkSum % 11;
 var verify = remaining == 0 ? 0 : 11 - remaining;
 return verify == checkDigit || (verify == 10 && checkDigit == 'A');
}

在網(wǎng)上找了很久都沒合意的驗證方式,最后通過Google找到一個國外寫的js驗證,發(fā)現(xiàn)可以使用。

上面那段驗證的很精密,包含身份證真實性的校驗,如果只是想驗證輸入的香港身份證格式,請使用下面的這段js。

function IsHKID(str) {
 var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 // basic check length
 if (str.length < 8)
 return false;
 // handling bracket
 if (str.charAt(str.length-3) == '(' && str.charAt(str.length-1) == ')')
 str = str.substring(0, str.length - 3) + str.charAt(str.length -2);
 // convert to upper case
 str = str.toUpperCase();
 // regular expression to check pattern and split
 var hkidPat = /^([A-Z]{1,2})([0-9]{6})([A0-9])$/;
 var matchArray = str.match(hkidPat);
 // not match, return false
 if (matchArray == null)
 return false;
 return true;
 }

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!

相關(guān)文章

最新評論