|
11 | 11 | * @website http://formvalidator.net/#location-validators |
12 | 12 | * @license MIT |
13 | 13 | */ |
14 | | -(function($) { |
| 14 | +(function ($) { |
15 | 15 |
|
16 | | - $.formUtils.registerLoadedModule('date'); |
| 16 | + $.formUtils.registerLoadedModule('date'); |
17 | 17 |
|
18 | | - /* |
19 | | - * Validate time hh:mm |
20 | | - */ |
21 | | - $.formUtils.addValidator({ |
22 | | - name : 'time', |
23 | | - validatorFunction : function(time) { |
24 | | - if (time.match(/^(\d{2}):(\d{2})$/) === null) { |
25 | | - return false; |
26 | | - } else { |
27 | | - var hours = parseInt(time.split(':')[0],10); |
28 | | - var minutes = parseInt(time.split(':')[1],10); |
29 | | - if( hours > 23 || minutes > 59 ) { |
30 | | - return false; |
31 | | - } |
32 | | - } |
33 | | - return true; |
34 | | - }, |
35 | | - errorMessage : '', |
36 | | - errorMessageKey: 'badTime' |
37 | | - }); |
| 18 | + /* |
| 19 | + * Validate time hh:mm |
| 20 | + */ |
| 21 | + $.formUtils.addValidator({ |
| 22 | + name: 'time', |
| 23 | + validatorFunction: function (time) { |
| 24 | + if (time.match(/^(\d{2}):(\d{2})$/) === null) { |
| 25 | + return false; |
| 26 | + } else { |
| 27 | + var hours = parseInt(time.split(':')[0], 10); |
| 28 | + var minutes = parseInt(time.split(':')[1], 10); |
| 29 | + if (hours > 23 || minutes > 59) { |
| 30 | + return false; |
| 31 | + } |
| 32 | + } |
| 33 | + return true; |
| 34 | + }, |
| 35 | + errorMessage: '', |
| 36 | + errorMessageKey: 'badTime' |
| 37 | + }); |
38 | 38 |
|
39 | | - /* |
40 | | - * Is this a valid birth date |
41 | | - */ |
42 | | - $.formUtils.addValidator({ |
43 | | - name : 'birthdate', |
44 | | - validatorFunction : function(val, $el, conf) { |
45 | | - var dateFormat = 'yyyy-mm-dd'; |
46 | | - if($el.valAttr('format')) { |
47 | | - dateFormat = $el.valAttr('format'); |
48 | | - } |
49 | | - else if(typeof conf.dateFormat !== 'undefined') { |
50 | | - dateFormat = conf.dateFormat; |
51 | | - } |
| 39 | + /* |
| 40 | + * Is this a valid birth date |
| 41 | + */ |
| 42 | + $.formUtils.addValidator({ |
| 43 | + name: 'birthdate', |
| 44 | + validatorFunction: function (val, $el, conf) { |
| 45 | + var dateFormat = 'yyyy-mm-dd'; |
| 46 | + if ($el.valAttr('format')) { |
| 47 | + dateFormat = $el.valAttr('format'); |
| 48 | + } |
| 49 | + else if (typeof conf.dateFormat !== 'undefined') { |
| 50 | + dateFormat = conf.dateFormat; |
| 51 | + } |
52 | 52 |
|
53 | | - var inputDate = $.formUtils.parseDate(val, dateFormat); |
54 | | - if (!inputDate) { |
55 | | - return false; |
56 | | - } |
| 53 | + var inputDate = $.formUtils.parseDate(val, dateFormat); |
| 54 | + if (!inputDate) { |
| 55 | + return false; |
| 56 | + } |
57 | 57 |
|
58 | | - var d = new Date(); |
59 | | - var currentYear = d.getFullYear(); |
60 | | - var year = inputDate[0]; |
61 | | - var month = inputDate[1]; |
62 | | - var day = inputDate[2]; |
| 58 | + var year = inputDate[0], |
| 59 | + month = inputDate[1], |
| 60 | + day = inputDate[2], |
| 61 | + age = getAge(year, month, day), |
| 62 | + allowedAgeRange = ($el.valAttr('age-range') || '0-124').split('-'); |
63 | 63 |
|
64 | | - if (year === currentYear) { |
65 | | - var currentMonth = d.getMonth() + 1; |
66 | | - if (month === currentMonth) { |
67 | | - var currentDay = d.getDate(); |
68 | | - return day <= currentDay; |
69 | | - } |
70 | | - else { |
71 | | - return month < currentMonth; |
72 | | - } |
73 | | - } |
74 | | - else { |
75 | | - return year < currentYear && year > (currentYear - 124); // we can not live for ever yet... |
76 | | - } |
77 | | - }, |
78 | | - errorMessage : '', |
79 | | - errorMessageKey: 'badDate' |
80 | | - }); |
| 64 | + if (allowedAgeRange.length !== 2 || !$.isNumeric(allowedAgeRange[0]) || !$.isNumeric(allowedAgeRange[1])) { |
| 65 | + throw new Error('Date range format invalid'); |
| 66 | + } |
| 67 | + |
| 68 | + return age >= allowedAgeRange[0] && age <= allowedAgeRange[1]; |
| 69 | + }, |
| 70 | + errorMessage: '', |
| 71 | + errorMessageKey: 'badDate' |
| 72 | + }); |
| 73 | + |
| 74 | + |
| 75 | + function getAge(otherDateYear, otherDateMonth, otherDateDay) { |
| 76 | + var otherDate = new Date(), |
| 77 | + nowDate = new Date(); |
| 78 | + otherDate.setYear(otherDateYear); |
| 79 | + otherDate.setMonth(otherDateMonth); |
| 80 | + otherDate.setDate(otherDateDay); |
| 81 | + return new Date(nowDate.getTime() - otherDate.getTime()).getUTCFullYear() - 1970; |
| 82 | + } |
81 | 83 |
|
82 | 84 | })(jQuery); |
0 commit comments