-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.js
More file actions
65 lines (62 loc) · 2.65 KB
/
Copy pathdate.js
File metadata and controls
65 lines (62 loc) · 2.65 KB
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
(function ($) {
$.fn.dateFormat = function (options) {
var params = $.extend({
format: 'xx-xx-xxxx',
}, options);
if (params.format === 'xx-xx-xxxx') {
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent.clipboardData.getData('Text');
if (!$.isNumeric(inputValue)) {
return false;
} else {
inputValue = String(inputValue.replace(/(\d{2})(\d{2})(\d{4})/, "$1-$2-$3"));
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 10);
$(this).val(inputValue);
}
});
$(this).on('keyup', function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 2 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
} else if (curchr == 5 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
}
$(this).attr('maxlength', '10');
});
} else if (params.format === 'xx/xx/xxxx') {
$(this).on('keyup', function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 2 && e.which != 8 && e.which != 0) {
$(this).val(curval + "/");
} else if (curchr == 5 && e.which != 8 && e.which != 0) {
$(this).val(curval + "/");
}
$(this).attr('maxlength', '10');
});
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent.clipboardData.getData('Text');
if (!$.isNumeric(inputValue)) {
return false;
} else {
inputValue = String(inputValue.replace(/(\d{2})(\d{2})(\d{4})/, "$1/$2/$3"));
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 10);
$(this).val(inputValue);
}
});
}
}
}(jQuery));