This repository has been archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
202 lines (181 loc) · 5.85 KB
/
scripts.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
let errorMsg = document.getElementById("errorMsg");
function validate() {
if (checkName() == false) {
return;
} else if (checkEmail() == false) {
return;
} else if (checkUser() == false) {
return;
} else if (checkPass() == false) {
return;
} else if (confirmPass() == false) {
return;
} else if (checkGender() == false) {
return;
} else if (checkQty() == false) {
return;
} else if (checkAddress() == false) {
return;
} else if (checkAgreement() == false) {
return;
} else {
errorMsg.innerHTML = "";
alert("Submit Success!")
}
resetForm();
}
function checkName() {
name = document.getElementById("inputName").value;
if (name.length == 0) {
errorMsg.innerHTML = "Name must be filled!";
return false;
}
let containsSymbol = false;
for (let i = 0; i < name.length; i++) {
let checkChar = name.charCodeAt(i);
if (checkChar != 32) {
if (checkChar <= 64 || checkChar >= 91 && checkChar <= 96 || checkChar >= 123) {
containsSymbol = true;
}
}
}
if (containsSymbol == true) {
errorMsg.innerHTML = "Name must be alphabetical only!";
return false;
} else if (containsSymbol == false) {
errorMsg.innerHTML = "";
return true;
}
}
function checkEmail() {
email = document.getElementById("inputEmail").value;
if (email.length == 0) {
errorMsg.innerHTML = "Email must be filled!";
return false;
}
containsSymbol = false;
for (let i = 0; i < email.length; i++) {
checkChar = email.charCodeAt(i);
if (checkChar != 64 && checkChar != 46) {
if (checkChar < 48 || checkChar > 57 && checkChar < 64 || checkChar > 122) {
containsSymbol = true;
}
}
}
if (containsSymbol == true) {
errorMsg.innerHTML = "Email cannot contain symbols other than '@' and '.'!";
return false;
} else if (!email.includes("@") || !email.includes(".")) {
errorMsg.innerHTML = "Email must contain '@' and '.' symbols!";
return false;
} else if (email.includes("@.") || email.includes(".@")) {
errorMsg.innerHTML = "'@' and '.' symbols cannot be side by side in email!";
return false;
} else {
errorMsg.innerHTML = "";
return true;
}
}
function checkUser() {
user = document.getElementById("inputUser").value;
if (user.length == 0 || user.length < 6) {
errorMsg.innerHTML = "Username must be greater than 6 characters!";
return false;
} else if (user.includes(" ")) {
errorMsg.innerHTML = "Username cannot contain space character!";
return false;
} else {
errorMsg.innerHTML = "";
return true;
}
}
function checkPass() {
pass = document.getElementById("inputPass").value;
if (pass.length < 8) {
errorMsg.innerHTML = "Password must be greater than 8 characters!";
return false;
}
let containsNum = false;
let containsAlpha = false;
for (let i = 0; i < pass.length; i++) {
checkChar = pass.charCodeAt(i);
if (checkChar > 47 && checkChar < 58) {
containsNum = true;
} else if (checkChar > 64 && checkChar < 91 || checkChar > 96 && checkChar < 123) {
containsAlpha = true;
}
}
if (!containsNum || !containsAlpha) {
errorMsg.innerHTML = "Password must be alphanumeric!";
return false;
} else {
errorMsg.innerHTML = "";
return true;
}
}
function confirmPass() {
pass = document.getElementById("inputPass").value;
passCopy = document.getElementById("inputPassConf").value;
if (passCopy != pass) {
errorMsg.innerHTML = "Passwords do not match!";
return false;
} else {
errorMsg.innerHTML = "";
return true;
}
}
function checkGender() {
let genderF = document.getElementById("radioF");
let genderM = document.getElementById("radioM");
let genderO = document.getElementById("radioO");
if (!genderF.checked && !genderM.checked && !genderO.checked) {
errorMsg.innerHTML = "Gender must be selected!";
return false;
} else {
errorMsg.innerHTML = "";
return true
}
}
function checkQty() {
qty = document.getElementById("inputQty").value;
if (qty == 0) {
errorMsg.innerHTML = "Quantity must be filled/greater than 0!";
return false;
} else {
errorMsg.innerHTML = "";
return true;
}
}
function checkAddress() {
address = document.getElementById("inputAddress").value;
if (!address.endsWith(" Street")) {
errorMsg.innerHTML = "Address must end with ' Street!'";
return false;
} else {
errorMsg.innerHTML = "";
return true;
}
}
function checkAgreement() {
if (!document.getElementById("inputAgree").checked) {
errorMsg.innerHTML = "User Agreement must be checked!";
return false;
} else {
errorMsg.innerHTML = "";
return true;
}
}
function resetForm() {
document.getElementById("inputName").value = "";
document.getElementById("inputEmail").value = "";
document.getElementById("inputUser").value = "";
document.getElementById("inputPass").value = "";
document.getElementById("inputPassConf").value = "";
document.getElementById("radioF").checked = false;
document.getElementById("radioM").checked = false;
document.getElementById("radioO").checked = false;
document.getElementById("inputQty").value = "";
document.getElementById("inputAddress").value = "";
document.getElementById("inputAgree").checked = false;
errorMsg.innerHTML = "Form reset!";
}