Your location:Tech News>News>Script>15/18 jQuery using regular authentication ID exemplary method

Latest News

15/18 jQuery using regular authentication ID exemplary method

Author:fsadmin

Views:

  // ID check

  var vcity = {11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Journal of Shanxi", 15: "Inner Mongolia"

  21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu"

  33: "Zhejiang", 34: "Journal", 35: "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan"

  42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing"

  51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi", 62: "Gansu"

  63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"

  };

  function checkCard (card) {

  // is empty

  if (!card ||!isCardNo (card) ||!checkProvince (card) ||!checkBirthday (card) ||!checkParity (card)) {

  ZlyJs.divTip ( "Please enter the correct ID number");

  jQuery ( ".sf_list ").focus ();

  return false;

  }

  return true;

  };

  // Check whether the number of specifications, including length, type

  function isCardNo (card) {

  // ID number is 15 or 18, 15 when all numbers, 18 to 17 before the number, the last digit is a check digit, it may be numbers or characters X

  var reg = / (^ \ d {15} $) | (^ \ d {17} (\ d | X) $) /;

  if (reg.test (card) === false) {

  return false;

  }

  return true;

  };

  // get the first two identity cards, check provinces

  function checkProvince (card) {

  var province = card.substr (0,2);

  if (vcity [province] == undefined) {

  return false;

  }

  return true;

  };

  // Check whether the correct date of birth

  function checkBirthday (card) {

  var len = card.length;

  // When the ID card 15, the order of the province (3) City (3) years (2) February (2) Day (2) parity bit (3), are all digital

  if (len == ''15'') {

  var re_fifteen = / ^ (\ d {6}) (\ d {2}) (\ d {2}) (\ d {2}) (\ d {3}) $ /;

  var arr_data = card.match (re_fifteen);

  var year = arr_data [2];

  var month = arr_data [3];

  var day = arr_data [4];

  var birthday = new Date ( ''19'' + year + ''/'' + month + ''/'' + day);

  return verifyBirthday ( ''19'' + year, month, day, birthday);

  }

  When // ID 18, the order of the province (3) City (3) years (four) months (2) Day (2) parity bit (4), the end of the check bits may be X

  if (len == ''18'') {

  var re_eighteen = / ^ (\ d {6}) (\ d {4}) (\ d {2}) (\ d {2}) (\ d {3}) ([0-9] | X) $ /;

  var arr_data = card.match (re_eighteen);

  var year = arr_data [2];

  var month = arr_data [3];

  var day = arr_data [4];

  var birthday = new Date (year + ''/'' + month + ''/'' + day);

  return verifyBirthday (year, month, day, birthday);

  }

  return false;

  };

  // check date

  function verifyBirthday (year, month, day, birthday) {

  var now = new Date ();

  var now_year = now.getFullYear ();

  // date is reasonable

  if (birthday.getFullYear () == year && (birthday.getMonth () + 1) == month && birthday.getDate () == day) {

  // year judgment range (between 100 years to 3 years)

  var time = now_year - year;

  if (time> = 3 && time <=100)

  {

  return true;

  }

  return false;

  }

  return false;

  };

  // detecting check bits

  function checkParity (card) {

  // 15 rpm 18

  card = changeFivteenToEighteen (card);

  var len = card.length;

  if (len == ''18'') {

  var arrInt = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

  var arrCh = new Array ( ''1'', ''0'', ''X'', ''9'', ''8'', ''7'', ''6'', ''5'', ''4'', ''3'', ''2'') ;

  var cardTemp = 0, i, valnum;

  for (i = 0; i < 17; i ++){

  cardTemp + = card.substr (i, 1) * arrInt [i];

  }

  valnum = arrCh [cardTemp% 11];

  if (valnum == card.substr (17, 1)) {

  return true;

  }

  return false;

  }

  return false;

  };

  // 15 rpm 18 ID number

  function changeFivteenToEighteen (card) {

  if (card.length == ''15'') {

  var arrInt = new array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

  var arrCh = new array ( ''1'', ''0'', ''X'', ''9'', ''8'', ''7'', ''6'', ''5'', ''4'', ''3'', ''2'') ;

  var cardTemp = 0, i;

  card = card.substr (0, 6) + ''19'' + card.substr (6, card.length - 6);

  for (i = 0; i < 17; i ++){

  cardTemp + = card.substr (i, 1) * arrInt [i];

  }

  card + = arrCh [cardTemp% 11];

  return card;

  }

  return card;

  };

  if (!checkCard (code)) {

  alert ( "Please enter the correct ID number");

  return false;

  }

Recommend article

Relate article