-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathsignup.js
132 lines (111 loc) · 4.18 KB
/
signup.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
128
129
130
131
132
// Regular expression for name validation
const nameRegex = /^[A-Za-z\s-]{2,50}$/;
// Function to validate input fields
function validateNameField(inputField, fieldName) {
const value = inputField.value.trim();
const errorId = `${inputField.id}-error`;
let errorDiv = document.getElementById(errorId);
// Create error div if it doesn't exist
if (!errorDiv) {
errorDiv = document.createElement("div");
errorDiv.id = errorId;
errorDiv.className = "error-message";
errorDiv.style.color = "red";
errorDiv.style.fontSize = "12px";
errorDiv.style.marginTop = "5px";
inputField.parentNode.appendChild(errorDiv);
}
// Validation checks
if (value.length === 0) {
errorDiv.textContent = `${fieldName} is required.`;
inputField.style.borderColor = "red";
return false;
} else if (value.length < 2) {
errorDiv.textContent = `${fieldName} must be at least 2 characters long.`;
inputField.style.borderColor = "red";
return false;
} else if (value.length > 50) {
errorDiv.textContent = `${fieldName} must not exceed 50 characters.`;
inputField.style.borderColor = "red";
return false;
} else if (!nameRegex.test(value)) {
errorDiv.textContent = `${fieldName} can only contain letters, spaces, and hyphens.`;
inputField.style.borderColor = "red";
return false;
} else {
errorDiv.textContent = "";
inputField.style.borderColor = "initial";
return true;
}
}
// Add event listeners when the document loads
document.addEventListener("DOMContentLoaded", function () {
const nameInput = document.getElementById("name");
const surnameInput = document.getElementById("surname");
const passwordInput=document.getElementById("password");
const confirmPasswordInput=document.getElementById("confirm-password");
const form = document.querySelector("form");
// Real-time validation
nameInput.addEventListener("input", function () {
validateNameField(this, "Name");
});
surnameInput.addEventListener("input", function () {
validateNameField(this, "Surname");
});
passwordInput.addEventListener("input", function(){
validatePasswordFields(passwordInput, confirmPasswordInput);
});
confirmPasswordInput.addEventListener("input", function(){
validatePasswordFields(passwordInput, confirmPasswordInput);
});
function validatePasswordFields(passwordInput, confirmPasswordInput){
const password=passwordInput.value.trim();
const confirmPassword=confirmPasswordInput.value.trim();
const errorId=`${confirmPasswordInput.id}-error`;
let errorDiv=document.getElementById(errorId);
if(!errorDiv){
errorDiv=document.createElement("div");
errorDiv.id=errorId;
errorDiv.className="error-message";
errorDiv.style.color="red";
errorDiv.style.fontSize="12px";
errorDiv.style.marginTop="5px";
confirmPasswordInput.parentNode.appendChild(errorDiv);
}
if(password.length===0 || confirmPassword.length===0){
errorDiv.textContent="Password and Confirm password are required";
confirmPasswordInput.style.borderColor="red";
return false;
}
else if(password.length<6){
errorDiv.textContent="Password must be atleast 6 characters long";
passwordInput.style.borderColor="red";
return false;
}
else if(password.length>12){
errorDiv.textContent="Password must not exceed 12 characters";
confirmPasswordInput.style.borderColor="red";
return false;
}
else if(password!==confirmPassword){
errorDiv.textContent="Passwords do not match";
confirmPasswordInput.style.borderColor="red";
return false;
}
else{
errorDiv.textContent="";
passwordInput.style.borderColor="initial";
confirmPasswordInput.style.borderColor="initial";
return true;
}
}
// Form submission validation
form.addEventListener("submit", function (event) {
const isNameValid = validateNameField(nameInput, "Name");
const isSurnameValid = validateNameField(surnameInput, "Surname");
const isPasswordValid=validatePasswordFields(passwordInput, confirmPasswordInput);
if (!isNameValid || !isSurnameValid || !isPasswordValid) {
event.preventDefault();
}
});
});