This repository has been archived by the owner on Jul 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
125 lines (114 loc) · 3.84 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
"use strict";
const submitBtn = document.getElementById("submit-info");
//USERNAME
const userName = document.querySelector(".first-name");
const nameLabel = document.querySelector(".name-label");
const nameError = document.querySelector(".first-name-error");
//LASTNAME
const userLastName = document.querySelector(".last-name");
const lastNameLabel = document.querySelector(".lastname-label");
const lastNameError = document.querySelector(".last-name-error");
//EMAIL
const userEmail = document.querySelector(".user-email");
const emailLabel = document.querySelector(".email-label");
const emailError = document.querySelector(".user-email-error");
//PASS
const userPass = document.querySelector(".user-pass");
const passLabel = document.querySelector(".pass-label");
const passError = document.querySelector(".user-pass-error");
const successMsg = document.querySelector(".success-msg");
let emailValidation = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
let passValidation = /^[A-Za-z]\w{7,14}$/;
let errorFunc = function () {
if (userName.value.length === 0) {
nameLabel.textContent = "First Name cannot be empty";
userName.style.borderColor = "red";
nameError.style.display = "inline";
}
if (userLastName.value.length === 0) {
lastNameLabel.textContent = "Last Name cannot be empty";
userLastName.style.borderColor = "red";
lastNameError.style.display = "inline";
}
if (userName.value.length > 0) {
nameLabel.textContent = "";
userName.style.borderColor = "var(--grayish-blue)";
nameError.style.display = "none";
}
if (userLastName.value.length > 0) {
lastNameLabel.textContent = "";
userLastName.style.borderColor = "var(--grayish-blue)";
lastNameError.style.display = "none";
}
if (userEmail.value.length === 0) {
emailLabel.textContent = "Email cannot be empty";
userEmail.style.borderColor = "red";
emailError.style.display = "inline";
}
if (!userEmail.value.match(emailValidation) && userEmail.value.length !== 0) {
emailLabel.textContent = "Looks like this is not an email";
userEmail.style.borderColor = "red";
emailError.style.display = "inline";
}
if (userEmail.value.match(emailValidation)) {
emailLabel.textContent = "";
userEmail.style.borderColor = "var(--grayish-blue)";
emailError.style.display = "none";
}
if (userPass.value.length === 0) {
passLabel.textContent = "Password cannot be empty";
userPass.style.borderColor = "red";
passError.style.display = "inline";
}
if (!userPass.value.match(passValidation) && userPass.value.length !== 0) {
passLabel.textContent =
"A password must be between 7-16 characters that starts with a letter(Only letters, numbers & underscore)";
userPass.style.borderColor = "red";
passError.style.display = "inline";
}
if (userPass.value.match(passValidation)) {
passLabel.textContent = "";
userPass.style.borderColor = "var(--grayish-blue)";
passError.style.display = "none";
}
if (
userName.value.length > 0 &&
userLastName.value.length > 0 &&
userEmail.value.match(emailValidation) &&
userPass.value.match(passValidation)
) {
successMsg.textContent = "Success! Please check your email";
} else {
console.log("Unkown error");
}
};
//CLick with mouse
submitBtn.addEventListener("click", function (e) {
e.preventDefault();
errorFunc();
});
//Click with Enter
userName.addEventListener("keyup", function (e) {
if (e.keyCode === 13) {
e.preventDefault();
errorFunc();
}
});
userLastName.addEventListener("keyup", function (e) {
if (e.keyCode === 13) {
e.preventDefault();
errorFunc();
}
});
userEmail.addEventListener("keyup", function (e) {
if (e.keyCode === 13) {
e.preventDefault();
errorFunc();
}
});
userPass.addEventListener("keyup", function (e) {
if (e.keyCode === 13) {
e.preventDefault();
errorFunc();
}
});