Skip to content

Commit cef586c

Browse files
committed
victorjonsson#320 Added support for age-range in birthday validator
1 parent b754040 commit cef586c

File tree

2 files changed

+73
-70
lines changed

2 files changed

+73
-70
lines changed

src/modules/date.js

Lines changed: 63 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,74 @@
1111
* @website http://formvalidator.net/#location-validators
1212
* @license MIT
1313
*/
14-
(function($) {
14+
(function ($) {
1515

16-
$.formUtils.registerLoadedModule('date');
16+
$.formUtils.registerLoadedModule('date');
1717

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+
});
3838

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+
}
5252

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+
}
5757

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('-');
6363

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+
}
8183

8284
})(jQuery);

test/qunit.html

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -606,14 +606,15 @@
606606
{val:'2000-13-1', isValid:false},
607607
{val:'2000-1-1', isValid:false},
608608
{val:'-01-01', isValid:false},
609-
{val:input('01/01/2000', {'format':'dd/mm/yyyy', '':'date'}), isValid:true},
610-
{val:input('29/02/2000', {'format':'dd/mm/yyyy', '':'date'}), isValid:true},
611-
{val:input('29/02/2013', {'format':'dd/mm/yyyy', '':'date'}), isValid:false},
612-
{val:input('29/13/2013', {'format':'dd/mm/yyyy', '':'date'}), isValid:false},
613-
{val:input('29/00/2013', {'format':'dd/mm/yyyy', '':'date'}), isValid:false},
609+
{val:input('01/01/2000', {'format':'dd/mm/yyyy', '':'birthdate'}), isValid:true},
610+
{val:input('29/02/2000', {'format':'dd/mm/yyyy', '':'birthdate'}), isValid:true},
611+
{val:input('29/02/2013', {'format':'dd/mm/yyyy', '':'birthdate'}), isValid:false},
612+
{val:input('29/13/2013', {'format':'dd/mm/yyyy', '':'birthdate'}), isValid:false},
613+
{val:input('29/00/2013', {'format':'dd/mm/yyyy', '':'birthdate'}), isValid:false},
614614
{val:'2030-01-01', isValid:false}, // no future date
615-
{val:'1880-01-01', isValid:false} // no date that is to old
616-
];
615+
{val:'1880-01-01', isValid:false}, // no date that is to old
616+
{val:input('2000-01-01', {'age-range':'0-5', '':'birthdate'}), isValid:false},
617+
{val:input('2000-01-01', {'age-range':'17-100', '':'birthdate'}), isValid:true} ];
617618

618619
$.each(dates, function(i, obj) {
619620
runTest(obj, 'birthdate');
@@ -1062,7 +1063,7 @@
10621063
});
10631064

10641065
});
1065-
1066+
10661067
/*
10671068
* COMPLEXITY VALIDATION
10681069
*/
@@ -1107,7 +1108,7 @@
11071108
runTest(obj, 'complexity');
11081109
});
11091110
});
1110-
1111+
11111112
// TODO: Write more tests...
11121113
}
11131114

0 commit comments

Comments
 (0)