-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.maskedinput.js
executable file
·124 lines (104 loc) · 4.95 KB
/
jquery.maskedinput.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
// ==================================================
//
// jquery-input-mask-phone-number 1.0.14
//
// Licensed (The MIT License)
//
// Copyright © Raja Rama Mohan Thavalam <rajaram.tavalam@gmail.com>
//
// Last Updated On: 22/Aug/2020 IST 12:05 AM
//
// ==================================================
(function ($) {
$.fn.usPhoneFormat = function (options) {
var params = $.extend({
format: 'xxx-xxx-xxxx',
international: false,
}, options);
if (params.format === 'xxx-xxx-xxxx') {
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent && e.originalEvent.clipboardData.getData('Text');
inputValue = inputValue.replace(/\D/g, '');
if (!$.isNumeric(inputValue)) {
return false;
} else {
if (inputValue.length > 9) {
inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3"));
} else {
inputValue = String(inputValue.replace(/(\d{3})(?=\d)/g, '$1-'));
}
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 12);
$(this).val(inputValue);
}
});
$(this).on('keydown touchend', function (e) {
e = e || window.event;
var key = e.which || e.keyCode; // keyCode detection
var ctrl = e.ctrlKey || e.metaKey || key === 17; // ctrl detection
if (key == 86 && ctrl) { // Ctrl + V Pressed !
} else if (key == 67 && ctrl) { // Ctrl + C Pressed !
} else if (key == 88 && ctrl) { // Ctrl + x Pressed !
} else if (key == 65 && ctrl) { // Ctrl + a Pressed !
$(this).trigger("paste");
} else if (key != 9 && e.which != 8 && e.which != 0 && !(e.keyCode >= 96 && e.keyCode <= 105) && !(e.keyCode >= 48 && e.keyCode <= 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
} else if (curchr == 7 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
}
$(this).attr('maxlength', '12');
});
} else if (params.format === '(xxx) xxx-xxxx') {
$(this).on('keydown touchend', function (e) {
e = e || window.event;
var key = e.which || e.keyCode; // keyCode detection
var ctrl = e.ctrlKey || e.metaKey || key === 17; // ctrl detection
if (key == 86 && ctrl) { // Ctrl + V Pressed !
} else if (key == 67 && ctrl) { // Ctrl + C Pressed !
} else if (key == 88 && ctrl) { //Ctrl + x Pressed
} else if (key == 65 && ctrl) { //Ctrl + a Pressed !
$(this).trigger("paste");
} else if (key != 9 && e.which != 8 && e.which != 0 && !(e.keyCode >= 96 && e.keyCode <= 105) && !(e.keyCode >= 48 && e.keyCode <= 57)) {
return false;
}
var curchr = this.value.length;
var curval = $(this).val();
if (curchr == 3 && e.which != 8 && e.which != 0) {
$(this).val('+7(' + curval + ')');
} else if (curchr == 10 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
} else if (curchr == 13 && e.which != 8 && e.which != 0) {
$(this).val(curval + "-");
}
$(this).attr('maxlength', '16');
});
$(this).bind('paste', function (e) {
e.preventDefault();
var inputValue = e.originalEvent && e.originalEvent.clipboardData.getData('Text');
inputValue = inputValue.replace(/\D/g, '');
if (!$.isNumeric(inputValue)) {
return false;
} else {
if (inputValue.length > 9) {
inputValue = String(inputValue.replace(/(\d{3})(\d{3})(\d{4})/, "($1) $2-$3"));
} else if (inputValue.length > 6) {
inputValue = String(inputValue.replace(/(\d{3})(\d{3})(?=\d)/g, '($1) $2-'));
} else if (inputValue.length > 3) {
inputValue = String(inputValue.replace(/(\d{3})(?=\d)/g, '($1) '));
}
$(this).val(inputValue);
$(this).val('');
inputValue = inputValue.substring(0, 14);
$(this).val(inputValue);
}
});
}
}
}(jQuery));