-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (31 loc) · 1.03 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module.exports = function (input) {
if (typeof input === 'string') {
if (input.length === 11 &&
isValidFodselsdato(
input.slice(0,2),
input.slice(2,4),
getYearBorn(parseInt(input.slice(4,6), 10),
parseInt(input.slice(6,9), 10)))) {
return true;
}
}
return false;
};
function isValidFodselsdato(d, m, y) {
if(['01','03','05','07','08','10','12'].indexOf(m) > -1){
return inRange(d, 31);
} else if (['04','06','09','11'].indexOf(m) > -1) {
return inRange(d, 30);
} else if (m === '02') {
var leapYearDays = ((y % 4 === 0 && y % 100 !== 0) || y % 400 === 0) ? 29 : 28;
return inRange(d, leapYearDays);
} else {
return false;
}
}
function inRange(day, max) {
return day > 0 && day <= max;
}
function getYearBorn(year, individ) {
return individ < 500 ? year + 1900 : year + 2000;
}