-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangular-dob-input.js
266 lines (212 loc) · 8.82 KB
/
angular-dob-input.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
angular.module('angular-dob-input', [])
.directive('dobInput', ['$filter', function($filter){
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, elem, attr, ctrl){
var _hasTextSelected = function($target) {
var ref;
if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== $target.prop('selectionEnd')) {
return true;
}
if (typeof document !== "undefined" && document !== null ? (ref = document.selection) != null ? typeof ref.createRange === "function" ? ref.createRange().text : void 0 : void 0 : void 0) {
return true;
}
return false;
};
elem.bind('keypress', function(e) {
// restrict DOB
var $target, digit, value;
$target = angular.element(e.currentTarget);
digit = String.fromCharCode(e.which);
if (!/^\d+$/.test(digit) && !e.meta && e.keyCode >= 46) {
e.preventDefault();
return;
}
if(_hasTextSelected($target)) {
return;
}
value = $target.val() + digit;
value = value.replace(/\D/g, '');
if (value.length > 8) {
e.preventDefault()
return;
}
});
elem.bind('keypress', function(e) {
// format DOB
var $target, digit, val;
digit = String.fromCharCode(e.which);
if (!/^\d+$/.test(digit) && !e.meta && e.keyCode >= 46) {
e.preventDefault();
return;
}
$target = angular.element(e.currentTarget);
val = $target.val() + digit;
if (/^\d$/.test(val) && (val !== '0' && val !== '1')) {
e.preventDefault();
return $target.val("0" + val + " / ");
}
else if (/^\d\d \/ \d$/.test(val) && (digit !== '0' && digit !== '1' && digit !== '2' && digit !== '3')) {
e.preventDefault();
return $target.val($target.val() + '0' + digit + ' / ');
}
else if (/^\d\d\d$/.test(val) && (digit !== '0' && digit !== '1' && digit !== '2' && digit !== '3')) {
e.preventDefault();
return $target.val($target.val() + ' / 0' + digit + ' / ');
}
else if (/^\d\d$/.test(val) || /^\d\d \/ \d\d$/.test(val)) {
e.preventDefault();
return $target.val("" + val + " / ");
}
});
elem.bind('keypress', function(e) {
// format forward slash
var $target, slash, val;
slash = String.fromCharCode(e.which);
if (slash !== '/') {
return;
}
$target = angular.element(e.currentTarget);
val = $target.val();
if (/^\d$/.test(val) && val !== '0') {
return $target.val("0" + val + " / ");
}
else if (/^\d\d \/ \d$/.test(val) && val.substring(val.length - 1) !== '0') {
return $target.val(val.substring(0, val.length - 1) + '0' + val.substring(val.length - 1) + " / ");
}
});
elem.bind('keypress', function(e) {
// format forward DOB
var $target, digit, val;
digit = String.fromCharCode(e.which);
if (!/^\d+$/.test(digit) && !e.meta && e.keyCode >= 46) {
return;
}
$target = angular.element(e.currentTarget);
val = $target.val();
if (/^\d\d$/.test(val) || /^\d\d \/ \d\d$/.test(val)) {
return $target.val("" + val + " / ");
}
});
elem.bind('keydown', function(e) {
//format back DOB
var $target, value;
if (e.meta) {
return;
}
$target = angular.element(e.currentTarget);
value = $target.val();
if (e.which !== 8) {
return;
}
if (($target.prop('selectionStart') != null) && $target.prop('selectionStart') !== value.length) {
return;
}
if (/\d(\s|\/)+$/.test(value)) {
e.preventDefault();
return $target.val(value.replace(/\d(\s|\/)*$/, ''));
}
else if (/\s\/\s?\d?$/.test(value)) {
e.preventDefault();
return $target.val(value.replace(/\s\/\s?\d?$/, ''));
}
});
var parseDOB = function(value){
var month, prefix, year, _ref;
value = value || '';
value = value.replace(/\s/g, '');
_ref = value.split('/', 3), month = _ref[0], day = _ref[1], year = _ref[2];
if ((year != null ? year.length : void 0) === 2 && /^\d+$/.test(year)) {
prefix = (new Date).getFullYear();
prefix = +year > +prefix.toString().slice(2, 4) ? +prefix.toString().slice(0, 2) - 1 : prefix.toString().slice(0, 2);
year = prefix + year;
}
month = parseInt(month, 10);
day = parseInt(day, 10);
year = parseInt(year, 10);
return {
month: month,
day: day,
year: year
};
}
ctrl.$parsers.push(function(value) {
// parse DOB
if (!valid(value)) {
ctrl.$setValidity('dob', false);
return undefined;
}
else {
ctrl.$setValidity('dob', true);
}
if(value != null) {
var obj = parseDOB(value);
var dob = new Date(obj.year, obj.month-1, obj.day);
return $filter('date')(dob, 'MM/dd/yyyy');
}
return null;
});
ctrl.$formatters.push(function(value) {
// get formatted DOB
if (!valid(value)) {
ctrl.$setValidity('dob', false);
return undefined;
}
else {
ctrl.$setValidity('dob', true);
}
if(value != null) {
var obj = parseDOB(value);
var dob = new Date(obj.year, obj.month-1, obj.day);
if (isNaN(dob.getTime())) {
return null;
}
return $filter('date')(dob, 'MM / dd / yyyy');
}
return null;
});
var valid = function(val) {
// valid if empty - let ng-required handle empty
if(val == null || val.length == 0) return true;
var obj = parseDOB(val);
var month = obj.month;
var day = obj.day;
var year = obj.year;
var currentTime, expiry, prefix;
if (!(month && day && year)) {
return false;
}
if (!/^\d+$/.test(month)) {
return false;
}
if (!/^\d+$/.test(day)) {
return false;
}
if (!/^\d+$/.test(year)) {
return false;
}
if (!(parseInt(month, 10) <= 12)) {
return false;
}
if (!(parseInt(day, 10) <= 31)) {
return false;
}
if (year.length === 2) {
var prefix = (new Date).getFullYear();
prefix = +year > +prefix.toString().slice(2, 4) ? +prefix.toString().slice(0, 2) - 1 : prefix.toString().slice(0, 2);
year = prefix + year;
}
if (year < 1900) {
return false;
}
var dob = new Date(year, month - 1, day);
if (!dob || dob.getDate() != day || dob.getFullYear() != year || dob.getMonth() != month - 1) {
return false;
}
var currentTime = new Date;
return dob < currentTime;
};
}
}
}]);