-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.js
More file actions
69 lines (61 loc) · 2.35 KB
/
validation.js
File metadata and controls
69 lines (61 loc) · 2.35 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
63
64
65
66
67
68
69
// ------------- Validation ----------------------
function validateForm() {
const form = document.getElementById("employeeForm");
const name = form.elements.name.value;
const dob = form.elements.dob.value;
const email = form.elements.email.value;
const phone = form.elements.phone.value;
const nameInput = document.getElementById("name");
const nameError = document.getElementById("name-error")
const dobInput = document.getElementById("dob");
const dobError = document.getElementById("dob-error")
const emailInput = document.getElementById("email");
const emailError = document.getElementById("email-error")
const phoneInput = document.getElementById("phone");
const phoneError = document.getElementById("phone-error")
let valid = true;
if (name.trim() === "") {
nameError.innerHTML = "Name is required";
nameInput.style.borderColor = "red";
valid = false;
} else if (name.length < 4 || name.length > 20) {
nameError.innerHTML = "Name must be between 4 and 20 characters";
nameInput.style.borderColor = "red";
valid = false;
} else {
nameError.innerHTML = "";
nameInput.style.borderColor = "green";
}
const currentDate = new Date();
const selectedDate = new Date(dob);
if (!dob || selectedDate >= currentDate) {
dobError.innerHTML = "Please enter a valid date of birth";
dobInput.style.borderColor = "red";
valid = false;
} else {
dobError.innerHTML = "";
dobInput.style.borderColor = "green";
}
if (!email.trim() || !validateEmail(email)) {
emailError.innerHTML = "Please enter a valid email address";
emailInput.style.borderColor = "red";
valid = false;
} else {
emailError.innerHTML = "";
emailInput.style.borderColor = "green";
}
const phonePattern = /^[0-9]{10}$/;
if (phone.trim() !== "" && !phone.match(phonePattern)) {
phoneError.innerHTML = "Please enter a 10-digit phone number";
phoneInput.style.borderColor = "red";
valid = false;
} else {
phoneError.innerHTML = "";
phoneInput.style.borderColor = "green";
}
return valid;
}
function validateEmail(email) {
const emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
return email.match(emailPattern);
}