bodlosh

Untitled

Aug 27th, 2020
1,214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function checkRC(rc, minAge) {
  2.         // TODO: Vyřešit případ prázdného rč
  3.         if (angular.isUndefined(minAge)) {
  4.             minAge = false;
  5.         }
  6.         var now = new Date();
  7.         var ym = now.getFullYear() - 2000;
  8.         var ypart;
  9.         var birthNum = rc + '',
  10.             valid = true;
  11.         if ((birthNum === '') || (birthNum === null)) {
  12.             valid = true;
  13.         } else if ((birthNum.length !== 10) && (birthNum.length !== 9)) {
  14.             valid = false;
  15.         } else if (birthNum.length === 10) {
  16.             ypart = parseInt(birthNum.substring(0, 2), 10);
  17.             if (ypart < 54 && ypart > ym) {
  18.                 valid = false;
  19.             } else {
  20.                 var num = parseInt(birthNum, 10) % 11;
  21.                 if (num !== 0 && !birthNum.endsWith('0000') && !birthNum.endsWith('9999')) {
  22.                     valid = false;
  23.                 }
  24.             }
  25.         } else if (birthNum.length === 9) {
  26.             ypart = parseInt(birthNum.substring(0, 2), 10);
  27.             if (ypart > 53) {
  28.                 valid = false;
  29.             }
  30.         }
  31.         if (valid) {
  32.             if (birthNum) {
  33.                 var day = parseInt(birthNum.substring(4, 6));
  34.                 var month = parseInt(birthNum.substring(2, 4));
  35.                 var year = parseInt('19' + birthNum.substring(0, 2));
  36.                 if (birthNum.substring(0, 2) <= ym) {
  37.                     year = parseInt('20' + birthNum.substring(0, 2));
  38.                 }
  39.                 var gender = 'M';
  40.                 if (month > 69) {
  41.                     month = month - 70;
  42.                     gender = 'F';
  43.                 } else if (month > 49) {
  44.                     month = month - 50;
  45.                     gender = 'F';
  46.                 } else if (month > 19) {
  47.                     month = month - 20;
  48.                 }
  49.  
  50.                 if (month > 12 || day > 31) {
  51.                     return {
  52.                         valid: false
  53.                     };
  54.                 }
  55.                 var mm = moment(year + '-' + month + '-' + day, 'YYYY-M-D');
  56.                 if (!mm.isValid()) {
  57.                     return {
  58.                         valid: false
  59.                     };
  60.                 }
  61.                 month = month - 1;
  62.                 var d = new Date(year, month, day);
  63.                 d.setHours(12);
  64.                 var d100 = new Date(year, month, day);
  65.                 d100.setHours(12);
  66.                 d100.setFullYear(d100.getFullYear() + 100);
  67.                 var today = new Date(), bdate;
  68.                 today.setHours(12);
  69.                 if ((d100 <= today) && (birthNum.length === 10) && (parseInt(birthNum.substring(0, 2)) < 54)) {
  70.                     bdate = d100;
  71.                 } else {
  72.                     bdate = d;
  73.                 }
  74.                 var ageDifMs = Date.now() - bdate.getTime();
  75.                 var ageDate = new Date(ageDifMs); // miliseconds from epoch
  76.                 var age = Math.abs(ageDate.getUTCFullYear() - 1970);
  77.                 if (minAge && age < minAge) {
  78.                     return {
  79.                         valid: false
  80.                     }
  81.                 }
  82.                 return {
  83.                     valid: true,
  84.                     gender: gender,
  85.                     age: age,
  86.                     datNar: bdate
  87.                 };
  88.             } else {
  89.                 return {
  90.                     valid: false
  91.                 };
  92.             }
  93.         } else {
  94.             return {
  95.                 valid: false
  96.             };
  97.         }
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment