-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
55 lines (47 loc) · 1.98 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
function validateForm() {
var username = document.getElementById("username").value.trim();
var email = document.getElementById("email").value.trim();
var password = document.getElementById("password").value;
var confirmPassword = document.getElementById("confirmPassword").value;
// Reset error messages
document.getElementById("usernameError").textContent = "";
document.getElementById("emailError").textContent = "";
document.getElementById("passwordError").textContent = "";
document.getElementById("confirmPasswordError").textContent = "";
var isValid = true;
// Validate username
if (username === "") {
document.getElementById("usernameError").textContent = "Username is required";
isValid = false;
}
// Validate email
if (email === "") {
document.getElementById("emailError").textContent = "Email is required";
isValid = false;
} else if (!isValidEmail(email)) {
document.getElementById("emailError").textContent = "Invalid email format";
isValid = false;
}
// Validate password
if (password === "") {
document.getElementById("passwordError").textContent = "Password is required";
isValid = false;
} else if (password.length < 6) {
document.getElementById("passwordError").textContent = "Password must be at least 6 characters";
isValid = false;
}
// Validate confirm password
if (confirmPassword === "") {
document.getElementById("confirmPasswordError").textContent = "Please confirm your password";
isValid = false;
} else if (confirmPassword !== password) {
document.getElementById("confirmPasswordError").textContent = "Passwords do not match";
isValid = false;
}
return isValid;
}
function isValidEmail(email) {
// Basic email validation using regex
var emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}