Repository navigation
Expand file tree
/
Copy pathcode.js
More file actions
94 lines (77 loc) · 2.92 KB
/
Copy pathcode.js
File metadata and controls
94 lines (77 loc) · 2.92 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
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
const uploadBox = document.getElementsByClassName("upload-box");
uploadBox.addEventListener("dragover", (e) => {
e.preventDefault();
});
uploadBox.addEventListener("drop", (e) => {
e.preventDefault();
const files = e.dataTransfer.files;
const file = files[0];
const reader = new FileReader();
reader.onload = (Event) => {
const imageDataURL = Event.target.result;
uploadBox.innerHTML = `<img src="${imageDataURL}" width="100%" height="100%">`;
};
reader.readAsDataURL(file);
});
/* =================================================================== */
// Select the form
// Select the form
const form = document.querySelector("form");
// Validation function (your method)
function validate() {
// Clear all error messages
document.getElementById("name-error").innerText = "";
document.getElementById("email-error").innerText = "";
document.getElementById("username-error").innerText = "";
document.getElementById("img-err").innerText = "";
// Get all input values
const fullName = document.getElementById("full-name").value.trim();
const emailAddress = document.getElementById("email").value.trim();
const userName = document.getElementById("github-username").value.trim();
let isValid = true;
// Validate fullname
if (fullName === "") {
document.getElementById("name-error").innerText = "Fullname is required";
isValid = false;
}
// Validate email
const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!emailPattern.test(emailAddress)) {
document.getElementById("email-error").innerText =
"Please enter a valid email address";
isValid = false;
}
// Validate github username
if (userName === "") {
document.getElementById("username-error").innerText =
"Github Username is required";
isValid = false;
}
return isValid; // Prevents form submission if invalid
}
// Add event listener for form submission
form.addEventListener("submit", function (event) {
// Run validation first
const isValid = validate();
// If validation fails, prevent submission
if (!isValid) {
event.preventDefault(); // Stop form submission
return;
}
// Get user input values after validation
const fullName = document.getElementById("full-name").value.trim();
const email = document.getElementById("email").value.trim();
const githubUsername = document
.getElementById("github-username")
.value.trim();
// Encode query parameters to handle special characters and spaces
const queryParams = new URLSearchParams({
fullName: encodeURIComponent(fullName),
email: encodeURIComponent(email),
githubUsername: encodeURIComponent(githubUsername),
});
// Redirect with the parameters
window.location.href = `welcome-page.html?${queryParams.toString()}`;
// Prevent default submission to allow custom behavior
event.preventDefault();
});