-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
55 lines (51 loc) · 1.62 KB
/
main.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
const firstName = document.getElementById("firstName");
const lastName = document.getElementById("lastName");
const email = document.getElementById("emailAddress");
const formPassword = document.getElementById("formPassword");
const form = document.querySelector("#form");
form.addEventListener("submit", (e) => {
e.preventDefault();
checkInputs();
});
const checkInputs = () => {
const firstNameValue = firstName.value;
const lastNameValue = lastName.value;
const emailValue = email.value;
const passwordValue = formPassword.value;
// checking the value of each input field and check them for error or success
if (firstNameValue === "") {
checkError(firstName, "First Name cannot be empty");
} else {
setSuccess(firstName);
}
if (lastNameValue === "") {
checkError(lastName, "Last Name cannot be empty");
} else {
setSuccess(lastName);
}
if (emailValue === "") {
checkError(email, "Looks like it is not an email");
} else if (!isEmail(emailValue)) {
checkError(email, "Email is not valid");
} else {
setSuccess(email);
}
if (passwordValue === "") {
checkError(formPassword, "Password cannot be empty");
} else {
setSuccess(formPassword);
}
};
const checkError = (input, message) => {
let formControl = input.parentElement;
let small = formControl.querySelector("small");
small.innerText = message;
formControl.className = "form_control error";
};
const setSuccess = (input) => {
let formControl = input.parentElement;
formControl.className = "form_control";
};
const isEmail = (email) => {
return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/i.test(email);
};