-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
62 lines (53 loc) · 2.19 KB
/
script.js
File metadata and controls
62 lines (53 loc) · 2.19 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
// dom elements
const form = document.getElementById("registration-form");
const username = document.getElementById("username");
const email = document.getElementById("email");
const password = document.getElementById("password");
const confirmPassword = document.getElementById("confirmPassword");
// form submission handler
form.addEventListener("submit", (e) => {
e.preventDefault();
const validations = [
checkRequired([username, email, password, confirmPassword]),
checkLength(username, 3, 15),
checkEmail(email),
checkLength(password, 6, 25),
checkPasswordsMatch(password, confirmPassword)
];
if (validations.every(v => v)) {
alert("Registration successful!");
form.reset();
document.querySelectorAll(".form-group").forEach(g => g.className = "form-group");
}
});
// check if passwords match
const checkPasswordsMatch = (input1, input2) =>
input1.value === input2.value ? true : (setStatus(input2, "error", "Passwords do not match"), false);
// validate email format
const checkEmail = (input) => {
const isValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(input.value.trim());
setStatus(input, isValid ? "success" : "error", isValid ? "" : "Email is not valid");
return isValid;
};
// check input length
const checkLength = (input, min, max) => {
const len = input.value.length;
const name = input.id.charAt(0).toUpperCase() + input.id.slice(1);
return len < min ? (setStatus(input, "error", `${name} must be at least ${min} characters`), false) :
len > max ? (setStatus(input, "error", `${name} must be less than ${max} characters`), false) :
(setStatus(input, "success"), true);
};
// check required fields
const checkRequired = (inputs) => {
let isValid = true;
inputs.forEach(input => {
const name = input.id.charAt(0).toUpperCase() + input.id.slice(1);
input.value.trim() === "" ? (setStatus(input, "error", `${name} is required`), isValid = false) : setStatus(input, "success");
});
return isValid;
};
// set input status (success/error)
const setStatus = (input, status, message = "") => {
input.parentElement.className = `form-group ${status}`;
if (message) input.parentElement.querySelector("small").innerText = message;
};