-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscript.js
More file actions
169 lines (136 loc) · 4.5 KB
/
script.js
File metadata and controls
169 lines (136 loc) · 4.5 KB
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
const usernameInput = document.getElementById(`username`);
const usernameText = document.getElementById(`usernameText`);
const emailInput = document.getElementById(`email`);
const emailText = document.getElementById(`emailText`);
const password1Input = document.getElementById(`password1`);
const password1Text = document.getElementById(`password1Text`);
const password2Input = document.getElementById(`password2`);
const password2Text = document.getElementById(`password2Text`);
const submitBtn = document.getElementById("submit");
const form = document.getElementById(`form`);
var letters = /^[A-Za-z0-9]+$/;
let originalPassword, username, email, copyPassword;
let idx;
usernameInput.addEventListener("input", (e) => {
username = e.target.value;
usernameVaildator();
});
function usernameVaildator() {
if (username == null || username.length === 0) {
usernameInput.classList.remove("valid");
usernameInput.classList.add(`invalid`);
usernameText.style.display = `block`;
usernameText.innerText = `Username can't be blank`;
return false;
} else if (!username.match(letters)) {
usernameInput.classList.remove("valid");
usernameInput.classList.add(`invalid`);
usernameText.style.display = `block`;
usernameText.innerText = `Username can't contains special characters`;
return false;
} else {
usernameInput.classList.remove("invalid");
usernameInput.classList.add(`valid`);
usernameText.style.display = `none`;
return true;
}
}
emailInput.addEventListener("input", (e) => {
email = e.target.value;
emailVaildator();
});
function emailVaildator() {
if (email == null) {
emailInput.classList.remove(`valid`);
emailInput.classList.add(`invalid`);
emailText.style.display = "block";
return false;
}
if (!countOccurrence1()) {
emailInput.classList.remove(`valid`);
emailInput.classList.add(`invalid`);
emailText.style.display = "block";
return false;
}
if (!countOccurrence2()) {
emailInput.classList.remove(`valid`);
emailInput.classList.add(`invalid`);
emailText.style.display = "block";
return false;
}
emailInput.classList.remove(`invalid`);
emailInput.classList.add(`valid`);
emailText.style.display = "none";
return true;
}
function countOccurrence2() {
let p = email.indexOf(".");
let canBe = false;
while (p !== -1) {
if (p > idx) canBe = true;
if (p === idx + 1) return false;
if (p === email.length - 1) return false;
let newIdx = email.indexOf(".", p + 1);
if (newIdx === p + 1) return false;
p = newIdx;
}
return canBe;
}
function countOccurrence1() {
let p = email.indexOf("@");
if (
p === -1 ||
p === 0 ||
p === email.length - 1 ||
email.indexOf("@", p + 1) !== -1
)
return false;
idx = p;
return true;
}
password1Input.addEventListener("input", (e) => {
originalPassword = e.target.value;
originalPasswordValidator();
});
function originalPasswordValidator() {
if (originalPassword == null || originalPassword.length < 8) {
password1Input.classList.remove(`valid`);
password1Input.classList.add(`invalid`);
password1Text.style.display = `block`;
return false;
} else {
password1Input.classList.remove(`invalid`);
password1Input.classList.add(`valid`);
password1Text.style.display = `none`;
return true;
}
}
password2Input.addEventListener("input", (e) => {
copyPassword = e.target.value;
copyPasswordValidator();
});
function copyPasswordValidator() {
if (copyPassword == null || originalPassword != copyPassword) {
password2Input.classList.remove(`valid`);
password2Input.classList.add(`invalid`);
password2Text.style.display = `block`;
return false;
} else {
password2Input.classList.remove(`invalid`);
password2Input.classList.add(`valid`);
password2Text.style.display = `none`;
return true;
}
}
form.addEventListener("submit", (e) => {
e.preventDefault();
let trueCount = 0;
if (usernameVaildator()) trueCount++;
if (emailVaildator()) trueCount++;
if (originalPasswordValidator()) trueCount++;
if (copyPasswordValidator()) trueCount++;
if (trueCount == 4) {
window.alert(`Form Sumitted Succesfully!`);
window.location.reload();
}
});