Skip to content

Instantly share code, notes, and snippets.

@jirihelmich
Last active October 13, 2020 10:18
Show Gist options
  • Save jirihelmich/cd934cc0a293f6507ef4 to your computer and use it in GitHub Desktop.
Save jirihelmich/cd934cc0a293f6507ef4 to your computer and use it in GitHub Desktop.
// PHP verze zde: https://phpfashion.com/jak-overit-platne-ic-a-rodne-cislo
function checkDate(m, d, y) {
// discuss at: http://phpjs.org/functions/checkdate/
// original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// improved by: Pyerre
// improved by: Theriault
// example 1: checkdate(12, 31, 2000);
// returns 1: true
// example 2: checkdate(2, 29, 2001);
// returns 2: false
// example 3: checkdate(3, 31, 2008);
// returns 3: true
// example 4: checkdate(1, 390, 2000);
// returns 4: false
return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate();
}
function birthNumber() {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$validators.birthNumber = function (modelValue, viewValue) {
if (ctrl.$isEmpty(modelValue)) {
// consider empty models to be valid
return true;
}
var regex = /^\s*(\d\d)(\d\d)(\d\d)[ /]*(\d\d\d)(\d?)\s*$/;
var match = regex.exec(viewValue);
if (match) {
var year = parseInt(match[1]);
var month = parseInt(match[2]);
var day = parseInt(match[3]);
var ext = parseInt(match[4]);
var c = match[5];
var mod;
if (c === '') {
year += year < 54 ? 1900 : 1800;
} else {
mod = ('' + year + month + day + ext) % 11;
if (mod === 10) { mod = 0; }
if (mod !== parseInt(c)) {
return false;
}
year += year < 54 ? 2000 : 1900;
}
if (month > 70 && year > 2003) {
month -= 70;
} else if (month > 50) {
month -= 50;
} else if (month > 20 && year > 2003) {
month -= 20;
}
return checkDate(month, day, year);
}
// it is invalid
return false;
};
}
};
}
@matuskrizo
Copy link

var ext = parseInt(match[4]); is not correct.
var ext = parseInt(match[4] + match[5]); this is correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment