-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
153 lines (128 loc) · 4.31 KB
/
script.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
"use strict";
const allCardFields = document.querySelectorAll("span[id^=card__]");
const allInputFields = document.querySelectorAll("input");
const inputForm = document.querySelector(".card__form");
const allErrorMsg = document.querySelectorAll(".error__msg");
const form = document.querySelector(".card__form");
const submitButton = document.querySelector("#submit");
const continueButton = document.querySelector("#continue");
const successMessage = document.querySelector(".message");
//This array stores the default card details
const cardDefText = [...allCardFields].flatMap((el) => {
return [...el.textContent.split("/")];
});
//This Symbol.split object allows the String.split method to split a string in fours
const splitByNum = {
[Symbol.split](str) {
let num = 4;
let pos = 0;
const result = [];
while (pos < str.length) {
const groupMatch = str.slice(pos, pos + num);
if (pos + num >= str.length) {
result.push(groupMatch);
break;
}
result.push(groupMatch);
pos += num;
}
return result;
},
};
//This Fn auto-updates the respective card fields
const updateFields = function (inputEl, cardSpan) {
cardSpan.textContent = inputEl.value || cardDefText[cardSpan.dataset.pos];
};
const stripSpace = (str) => str.replace(/ /g, "");
const groupFours = (text) => text.split(splitByNum).join(" ");
const updateText = function (inputEl, cardSpan) {
const remSpace = stripSpace(inputEl.value);
const inpId = inputEl.id;
switch (true) {
case inpId === "card_number":
if (remSpace.length > 16) {
inputEl.value = cardSpan.textContent;
break;
}
inputEl.value = groupFours(remSpace);
updateFields(inputEl, cardSpan);
break;
case inpId === "card_expiry-MM" ||
inpId === "card_expiry-YY" ||
inpId === "card_cvc":
if (remSpace.length > inputEl.size) {
inputEl.value = cardSpan.textContent;
break;
}
inputEl.value = remSpace;
updateFields(inputEl, cardSpan);
break;
default:
updateFields(inputEl, cardSpan);
break;
}
};
const successFn = function () {
successMessage.classList.toggle("hidden");
form.classList.toggle("hidden");
};
function errorCheck(e) {
e.preventDefault();
let noError = true; //State Variable to check if any errors occured
//Clear out any errors shown before
allErrorMsg.forEach((el) => (el.textContent = ""));
for (const el of allInputFields) {
const errorSibl =
document.querySelector(`#${el.id}~span`) ||
el.closest("div").querySelector(".error__msg");
const elText = el.value;
const stripText = stripSpace(elText);
//The regex replaces all alphabetic chars and spaces with an empty string
//The boolean is true if all chars are letters
const isAllText = elText.replace(/[\p{L} ]/gu, "") === "";
if (errorSibl.textContent !== "") continue;
switch (true) {
case stripText === "":
errorSibl.textContent = "Can't be blank";
noError = false;
break;
case el.dataset.lit === "number":
if (!isFinite(stripText)) {
errorSibl.textContent = "Wrong format, numbers only";
noError = false;
} else if (stripText.length !== el.size) {
errorSibl.textContent = `Must be ${el.size} digits long`;
noError = false;
}
break;
case el.dataset.lit === "text" && !isAllText:
errorSibl.textContent = "Wrong format, text only";
noError = false;
break;
default:
break;
}
}
if (noError) successFn();
}
function restart() {
allInputFields.forEach((el) => {
el.value = "";
const errorSibl =
document.querySelector(`#${el.id}~span`) ||
el.closest("div").querySelector(".error__msg");
console.log(errorSibl);
errorSibl.textContent = "";
const cardSpan = document.querySelector(`#card__${el.id}`);
cardSpan.textContent = cardDefText[cardSpan.dataset.pos];
});
successMessage.classList.toggle("hidden");
form.classList.toggle("hidden");
}
function inputGet() {
const cardElem = document.querySelector(`#card__${this.id}`);
updateText(this, cardElem);
}
allInputFields.forEach((el, i) => el.addEventListener("input", inputGet));
submitButton.addEventListener("click", errorCheck);
continueButton.addEventListener("click", restart);