-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.js
127 lines (113 loc) · 3.89 KB
/
validate.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
126
127
let nameError = document.getElementById("name-error");
let messageError = document.getElementById("message-error");
let emailError = document.getElementById("email-error");
//let phoneError = document.getElementById("phone-error");
let submitError = document.getElementById("submit-error");
function validateName() {
let name = document.getElementById("name").value;
if (name.length == 0) {
nameError.innerHTML = "name is required";
return false;
}
if (!name.match(/^[A-Za-z]*\s{1}[A-Za-z]*$/)) {
nameError.innerHTML = "Write full name";
return false;
}
nameError.innerHTML = "<i class='fas fa-check-circle'></i>";
return true;
}
function validateMessage() {
let message = document.getElementById("message").value;
console.log(message);
let required = 5;
let left = required - message.length;
if (left > 0) {
messageError.innerHTML = "add more characters";
return false;
}
messageError.innerHTML = "<i class='fas fa-check-circle'></i>";
}
function validateEmail() {
let email = document.getElementById("email").value;
if (email.length == 0) {
emailError.innerHTML = "Email is required";
return false;
}
if (!email.match(/^[A-Za-z\._\-[0-9]*[@][A-Za-z]*[\.][a-z]{2,4}$/)) {
emailError.innerHTML = "Invalid Email";
return false;
}
emailError.innerHTML = "<i class='fas fa-check-circle'></i>";
return true;
}
function validateForm() {
if (!validateName() || !validateEmail() || !validateMessage()) {
submitError.innerHTML = "Please fix error to submit";
return false;
}
}
document.getElementById("button").addEventListener("click", function (event) {
event.preventDefault();
console.log("hello");
validateName();
validateEmail();
validateMessage();
validateForm();
});
// Selecting form and input elements
const form = document.querySelector("form");
const passwordInput = document.getElementById("password");
const passToggleBtn = document.getElementById("pass-toggle-btn");
// Function to display error messages
const showError = (field, errorText) => {
field.classList.add("error");
const errorElement = document.createElement("small");
errorElement.classList.add("error-text");
errorElement.innerText = errorText;
field.closest(".form-group").appendChild(errorElement);
};
// Function to handle form submission
const handleFormData = (e) => {
e.preventDefault();
// Retrieving input elements
const fullnameInput = document.getElementById("fullname");
const emailInput = document.getElementById("email");
// Getting trimmed values from input fields
const fullname = fullnameInput.value.trim();
const password = passwordInput.value.trim();
const email = emailInput.value.trim();
// Regular expression pattern for email validation
const emailPattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/;
// Clearing previous error messages
document
.querySelectorAll(".form-group .error")
.forEach((field) => field.classList.remove("error"));
document
.querySelectorAll(".error-text")
.forEach((errorText) => errorText.remove());
// Performing validation checks
if (fullname === "") {
showError(fullnameInput, "Enter your full name");
}
if (password === "") {
showError(passwordInput, "Enter your password");
}
if (!emailPattern.test(email)) {
showError(emailInput, "Enter a valid email address");
}
// Checking for any remaining errors before form submission
const errorInputs = document.querySelectorAll(".form-group .error");
if (errorInputs.length > 0) return;
// Submitting the form
form.submit();
};
// Toggling password visibility
// passToggleBtn.addEventListener("click", () => {
// passToggleBtn.className =
// passwordInput.type === "password"
// ? "fa-solid fa-eye-slash"
// : "fa-solid fa-eye";
// passwordInput.type = passwordInput.type === "password" ? "text" : "password";
// });
// Handling form submission event
form.addEventListener("submit", handleFormData);