-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
79 lines (73 loc) · 2.64 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
const form = document.getElementById("form");
const password1Elem = document.getElementById("password1");
const password2Elem = document.getElementById("password2");
const messageContainer = document.querySelector(".message-container");
const message = document.getElementById("message");
const signupAnotherBtn = document.getElementById("signup-another-btn");
const gotoSigninBtn = document.getElementById("goto-signin-btn");
const header = document.querySelector("h1");
let isFormValid = false;
let isPassword2Valid = false;
let passwordsMatch = false;
function validatePassword() {
// Validate password using constraint API
isPassword2Valid = password2Elem.checkValidity();
passwordsMatch = password1Elem.value === password2Elem.value;
if (isPassword2Valid && passwordsMatch) {
password2Elem.style.borderColor = "limegreen";
} else {
password2Elem.style.borderColor = "red";
}
}
function storeFormData() {
const user = {
name: form.name.value,
phone: form.phone.value,
email: form.email.value,
website: form.website.value,
password: form.password.value,
};
console.log(user);
}
function processFormData(e) {
e.preventDefault();
// Validate form using constraint API
isFormValid = form.checkValidity();
validatePassword();
if (isFormValid && passwordsMatch) {
// Data is valid so submit
message.textContent = "Successfully registered";
message.style.color = "limegreen";
messageContainer.style.borderColor = "limegreen";
storeFormData();
form.setAttribute("hidden", true);
signupAnotherBtn.removeAttribute("hidden");
gotoSigninBtn.removeAttribute("hidden");
header.textContent = "Congratulations!";
} else if (isFormValid && !passwordsMatch) {
// Indicate that passwords don't match
message.textContent = "Make sure passwords match";
message.style.color = "red";
messageContainer.style.borderColor = "red";
} else {
// Indicte that data other than password is incorrectly entered
message.textContent = "Please fill out all fields correctly.";
message.style.color = "red";
messageContainer.style.borderColor = "red";
}
}
function returnToSignup() {
header.textContent = "Sign up today!";
signupAnotherBtn.setAttribute("hidden", true);
gotoSigninBtn.setAttribute("hidden", true);
form.reset();
validatePassword();
message.textContent = "Don't hesitate!";
message.style.color = "black";
messageContainer.style.borderColor = "black";
form.removeAttribute("hidden");
}
// Event listener
form.addEventListener("submit", processFormData);
password1Elem.addEventListener("input", validatePassword);
password2Elem.addEventListener("input", validatePassword);