Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function checkRC(rc, minAge) {
- // TODO: Vyřešit případ prázdného rč
- if (angular.isUndefined(minAge)) {
- minAge = false;
- }
- var now = new Date();
- var ym = now.getFullYear() - 2000;
- var ypart;
- var birthNum = rc + '',
- valid = true;
- if ((birthNum === '') || (birthNum === null)) {
- valid = true;
- } else if ((birthNum.length !== 10) && (birthNum.length !== 9)) {
- valid = false;
- } else if (birthNum.length === 10) {
- ypart = parseInt(birthNum.substring(0, 2), 10);
- if (ypart < 54 && ypart > ym) {
- valid = false;
- } else {
- var num = parseInt(birthNum, 10) % 11;
- if (num !== 0 && !birthNum.endsWith('0000') && !birthNum.endsWith('9999')) {
- valid = false;
- }
- }
- } else if (birthNum.length === 9) {
- ypart = parseInt(birthNum.substring(0, 2), 10);
- if (ypart > 53) {
- valid = false;
- }
- }
- if (valid) {
- if (birthNum) {
- var day = parseInt(birthNum.substring(4, 6));
- var month = parseInt(birthNum.substring(2, 4));
- var year = parseInt('19' + birthNum.substring(0, 2));
- if (birthNum.substring(0, 2) <= ym) {
- year = parseInt('20' + birthNum.substring(0, 2));
- }
- var gender = 'M';
- if (month > 69) {
- month = month - 70;
- gender = 'F';
- } else if (month > 49) {
- month = month - 50;
- gender = 'F';
- } else if (month > 19) {
- month = month - 20;
- }
- if (month > 12 || day > 31) {
- return {
- valid: false
- };
- }
- var mm = moment(year + '-' + month + '-' + day, 'YYYY-M-D');
- if (!mm.isValid()) {
- return {
- valid: false
- };
- }
- month = month - 1;
- var d = new Date(year, month, day);
- d.setHours(12);
- var d100 = new Date(year, month, day);
- d100.setHours(12);
- d100.setFullYear(d100.getFullYear() + 100);
- var today = new Date(), bdate;
- today.setHours(12);
- if ((d100 <= today) && (birthNum.length === 10) && (parseInt(birthNum.substring(0, 2)) < 54)) {
- bdate = d100;
- } else {
- bdate = d;
- }
- var ageDifMs = Date.now() - bdate.getTime();
- var ageDate = new Date(ageDifMs); // miliseconds from epoch
- var age = Math.abs(ageDate.getUTCFullYear() - 1970);
- if (minAge && age < minAge) {
- return {
- valid: false
- }
- }
- return {
- valid: true,
- gender: gender,
- age: age,
- datNar: bdate
- };
- } else {
- return {
- valid: false
- };
- }
- } else {
- return {
- valid: false
- };
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment