-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
188 lines (160 loc) · 6.16 KB
/
app.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
//dummy card elements
const dummyCardNumber = document.getElementById('dummy-card-number');
const dummyCardName = document.getElementById('dummy-card-name')
const dummyCardMonth = document.querySelector('#dummy-card-exp span:first-of-type');
const dummyCardYear = document.querySelector('#dummy-card-exp span:last-of-type');
const dummyCardCvc = document.getElementById('dummy-card-cvc');
// buttons
const confirmBtn = document.getElementById('confirm-btn');
//form input fields
const inputFieldName = document.getElementById('inputFieldName');
const inputFieldCardNumber = document.getElementById('inputFieldCardNumber');
const inputFieldMonth = document.getElementById('inputFieldMonth');
const inputFieldYear = document.getElementById('inputFieldYear');
const inputFieldCvc = document.getElementById('inputFieldCvc');
//sections
const completeState = document.querySelector('.complete-state');
const originalForm = document.querySelector('.form');
//functions
function changeDummyCardContent(dummyElement, formField) {
formField.addEventListener('keyup', event => {
let retVal;
// let newString;
if (formField === inputFieldCardNumber) {
if (formField.value.length === 4 ||
formField.value.length === 9 ||
formField.value.length === 14)
formField.value = formField.value.concat(' ')
}
if (dummyElement === dummyCardNumber) {
if (formField.value.trim().length < 16) {
retVal = formField.value + ('0'.repeat(16 - formField.value.trim().length));
// retVal = retVal.slice(0, 4) + ' ' + retVal.slice(4, 8) + ' ' + retVal.slice(8, 12) + ' ' + retVal.slice(12);
}
// else if (formField.value.length > dummyElement.textContent.length)
// retVal = formField.value.trim().substring(0, 18);
else if (isNaN(parseInt(formField.value)))
retVal = '0000 0000 0000 0000'
else
retVal = formField.value;
// retVal = retVal.slice(0, 4) + ' ' + retVal.slice(4, 8) + ' ' + retVal.slice(8, 12) + ' ' + retVal.slice(12);
} else
retVal = formField.value;
dummyElement.textContent = retVal;
})
}
function throwError(field, string) {
if (!field.classList.contains('error'))
field.classList.add('error');
// if (string === '')
// return;
if (field === inputFieldMonth || field === inputFieldYear) {
if (field.closest('div').querySelector('p'))
return;
} else if (field.closest('div').querySelector('p')) {
field.closest('div').querySelector('p').textContent = string;
return;
}
field.classList.add('error');
const p = document.createElement('p');
p.textContent = string;
p.classList.add('error-text');
field.closest('div').appendChild(p);
}
// function throwError2(field1, field2){
// }
function removeError(field) {
removeError2(field);
if (field.closest('div').querySelector('p')) {
// if (field === inputFieldMonth || field === inputFieldYear)
field.closest('div').removeChild(field.closest('div').querySelector('p'));
}
}
function removeError2(field) {
field.classList.remove('error');
}
function checkValidity(field) {
if (field.value.trim() === '') {
throwError(field, "Can't be blank");
return false;
} else if (isNaN(parseInt(field.value)) & field !== inputFieldName) {
throwError(field, 'Wrong format, numbers only');
return false;
} else {
removeError(field);
return true;
}
}
function checkValidity2(field1, field2) {
if (field1.value.trim() === '' & !(field2.value.trim() === '')) {
throwError(field1, "Can't be blank");
removeError2(field2);
return false;
} else if (!(field1.value.trim() === '') & field2.value.trim() === '') {
throwError(field2, "Can't be blank");
removeError2(field1);
return false;
} else if (field1.value.trim() === '' & field2.value.trim() === '') {
throwError(field1, "Can't be blank");
throwError(field2);
return false;
} else {
removeError(field1);
removeError(field2);
return true;
}
}
function confirmBtnHandler(event) {
event.preventDefault();
let validInputs;
validInputs = checkValidity(inputFieldName) &
checkValidity(inputFieldCardNumber) &
checkValidity(inputFieldCvc) &
checkValidity2(inputFieldMonth, inputFieldYear);
console.log(validInputs)
if (validInputs) {
originalForm.style.display = 'none';
completeState.style.display = 'flex';
const continueBtn = document.getElementById('continue-btn');
continueBtn.addEventListener('click', continueBtnHandler)
}
}
function clearInputs() {
for (const input of document.querySelectorAll('input'))
input.value = '';
}
function restoreDefaults() {
for (const input of document.querySelectorAll('input')) {
input.addEventListener('keyup', function() {
if (input.value.trim().length < 1)
resetDummyCard(input);
})
}
}
function resetDummyCard(field) {
if (field === inputFieldName)
dummyCardName.textContent = 'Jane Appleseed';
if (field === inputFieldCardNumber)
dummyCardNumber.textContent = '0000 0000 0000 0000';
if (field === inputFieldCvc)
dummyCardCvc.textContent = '000';
if (field === inputFieldMonth)
dummyCardMonth.textContent = '00';
if (field === inputFieldYear)
dummyCardYear.textContent = '00';
}
function continueBtnHandler() {
originalForm.style.display = 'flex';
completeState.style.display = 'none';
clearInputs();
resetDummyCard();
}
//function executions
changeDummyCardContent(dummyCardName, inputFieldName)
changeDummyCardContent(dummyCardNumber, inputFieldCardNumber)
changeDummyCardContent(dummyCardMonth, inputFieldMonth)
changeDummyCardContent(dummyCardYear, inputFieldYear)
changeDummyCardContent(dummyCardCvc, inputFieldCvc)
restoreDefaults();
confirmBtn.addEventListener('click', confirmBtnHandler);
// since continue btn is created after confirmBtnHandler runs successfully