Created
March 17, 2016 20:50
-
-
Save jirihelmich/039b943aa81cd9bea85d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = match[1]; | |
| var month = match[2]; | |
| var day = match[3]; | |
| var ext = 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; | |
| }; | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment