-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclsPhone.js
121 lines (105 loc) · 3.09 KB
/
clsPhone.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
class clsPhone
{
constructor(options)
{
if (!options)
{
throw 'Options must be defined for the phone class';
}
this.options = options;
//I don't need this twice do I?
this.containerElement = this.options.containerElement;
this.createElement();
}
createElement()
{
let containerContent;
this.isValid = true;
this.PhoneNumber = '';
//I think I can do this more simply now.
T.ajax
({
auth: true,
method: 'GET',
url: '/content/templates/TEAMclsPhone.html',
dataType: 'HTML',
success: function (result)
{
containerContent = result;
},
error: function (result)
{
}
});
if (typeof this.options.containerElement === 'undefined' ||
!this.options.containerElement)
{
throw 'options.containerElement must be defined: TEAMclsModal.js';
}
this.containerElement = this.options.containerElement;
this.containerElement.innerHTML = containerContent;
this.input =
this.containerElement.querySelector('.TEAMclsPhoneInput');
this.validationPopover =
this.containerElement.querySelector('.TEAMclsPhoneAlert');
//-----------------------------------------------------------
this.input.addEventListener('input',
this.ValidationTrigger.bind(this));
this.input.addEventListener('blur', this.FormatTrigger.bind(this));
}
ValidationTrigger()
{
this.PhoneNumber = this.input.value;
this.PhoneValidator(this.PhoneNumber);
}
FormatTrigger()
{
if (!this.isValid) return;
this.input.value = this.FormatPhone(this.input.value);
}
//-------------------------------------------------------------
PhoneValidator(numberToTest)
{
this.isValid = this.PhoneValidationTest(numberToTest);
const showValidation = numberToTest !== "" && !this.isValid;
this.ValidationDisplay(showValidation);
}
PhoneValidationTest(PhoneNumber)
{
return
/(^\D*\d{3}\D*\d{3}\D*\d{4}\D*$|^\d{4}$|^$)/.test(PhoneNumber);
}
ValidationDisplay(show)
{
if (show)
{
this.validationPopover.style.display = "inherit";
}
else
{
this.validationPopover.style.display = "none";
}
}
FormatPhone(PhoneNumber)
{
if (!PhoneNumber | PhoneNumber === '')
{
this.Reset();
return null;
}
const PhoneFormat = /^\D*(\d{3})\D*(\d{3})\D*(\d{4})\D*$/;
this.PhoneNumber = PhoneNumber.replace(PhoneFormat, '$1$2$3');
this.PhoneNumberFormatted = PhoneNumber.replace(PhoneFormat, '($1) $2-$3');
this.input.value = this.PhoneNumberFormatted;
this.PhoneValidator(this.input.value);
return this.PhoneNumberFormatted;
}
Reset()
{
this.input.value = '';
this.PhoneNumber = '';
this.PhoneNumberFormatted = '';
this.isValid = true;
this.ValidationDisplay();
}
}