Skip to content

Commit e79d7ec

Browse files
Merge pull request #92 from IshaSitlani/fix-validation
Fixed validation logic and download behavior
2 parents 8b04ee4 + 9bcd7db commit e79d7ec

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

script.js

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -175,71 +175,77 @@ function clearErrors() {
175175
span.textContent = '';
176176
});
177177
}
178+
function clearErrors() {
179+
const errorFields = document.querySelectorAll(".error-message");
180+
errorFields.forEach(field => field.textContent = "");
181+
}
178182

179183
function validateForm() {
180-
clearErrors(); // Clear any existing errors first
184+
clearErrors();
181185

182-
let isValid = true; // Assume valid until an error is found
186+
let isValid = true;
187+
let incompleteFields = [];
183188

184-
// Validate Full Name: Minimum 3 characters
185189
const nameInput = document.getElementById('input-name');
186190
if (nameInput.value.trim().length < 3) {
187191
document.getElementById('error-name').textContent = 'Full Name must be at least 3 characters.';
188192
isValid = false;
193+
incompleteFields.push("Full Name");
189194
}
190195

191-
// Validate Email: Basic email format (e.g., [email protected])
192196
const emailInput = document.getElementById('input-email');
193197
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
194198
if (!emailPattern.test(emailInput.value.trim())) {
195199
document.getElementById('error-email').textContent = 'Please enter a valid email address.';
196200
isValid = false;
201+
incompleteFields.push("Email");
197202
}
198203

199-
// Validate Phone Number: At least 10 digits (allowing optional + and spaces/hyphens)
200204
const phoneInput = document.getElementById('input-phone');
201205
const phonePattern = /^\+?[0-9\s-]{10,}$/;
202206
if (!phonePattern.test(phoneInput.value.trim())) {
203-
document.getElementById('error-phone').textContent = 'Please enter a valid phone number (at least 10 digits).';
207+
document.getElementById('error-phone').textContent = 'Please enter a valid phone number.';
204208
isValid = false;
209+
incompleteFields.push("Phone");
205210
}
206211

207-
// Validate LinkedIn Link (optional field, so only validate if value exists)
208212
const linkedinInput = document.getElementById('input-linkedin');
209213
const linkedinPattern = /^(https?:\/\/(www\.)?linkedin\.com\/in\/[a-zA-Z0-9_-]+\/?)$/i;
210214
if (linkedinInput.value.trim() && !linkedinPattern.test(linkedinInput.value.trim())) {
211-
document.getElementById('error-linkedin').textContent = 'Please enter a valid LinkedIn profile URL.';
215+
document.getElementById('error-linkedin').textContent = 'Please enter a valid LinkedIn URL.';
212216
isValid = false;
217+
incompleteFields.push("LinkedIn");
213218
}
214219

215-
// Validate GitHub Link (optional field, so only validate if value exists)
216220
const githubInput = document.getElementById('input-github');
217221
const githubPattern = /^(https?:\/\/(www\.)?github\.com\/[a-zA-Z0-9_-]+\/?)$/i;
218222
if (githubInput.value.trim() && !githubPattern.test(githubInput.value.trim())) {
219-
document.getElementById('error-github').textContent = 'Please enter a valid GitHub profile URL.';
223+
document.getElementById('error-github').textContent = 'Please enter a valid GitHub URL.';
220224
isValid = false;
225+
incompleteFields.push("GitHub");
221226
}
222227

223-
// Validate Professional Summary: Minimum 50 characters
224228
const aboutInput = document.getElementById('input-about');
225229
if (aboutInput.value.trim().length < 50) {
226230
document.getElementById('error-about').textContent = 'Professional Summary should be at least 50 characters.';
227231
isValid = false;
232+
incompleteFields.push("Professional Summary");
228233
}
229234

230-
return isValid;
235+
// If all good, return true
236+
if (isValid) return true;
237+
238+
// Else ask user if they want to proceed anyway
239+
return confirm("Please correct the highlighted errors in the form before downloading your resume.");
231240
}
232241

233242

234-
// --- PDF Download Button ---
243+
235244
document.getElementById("downloadBtn").addEventListener("click", () => {
236-
// Call validateForm() at the very beginning of the click handler
237-
if (!validateForm()) { // If validation returns false (meaning there are errors)
238-
alert("Please correct the highlighted errors in the form before downloading your resume.");
239-
return; // STOP the function execution here. This prevents the PDF download.
240-
}
245+
if (!validateForm()) return;
246+
247+
// --- PDF Download Button ---
241248

242-
// If validation passes (validateForm() returned true), then proceed with download
243249
const content = document.querySelector('#resume-sections');
244250
const options = {
245251
margin: 0.5,
@@ -249,44 +255,35 @@ document.getElementById("downloadBtn").addEventListener("click", () => {
249255
jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
250256
};
251257
html2pdf().from(content).set(options).save();
252-
});
253258

259+
})
254260

255261
// --- DOCX Download Button ---
256262
document.getElementById("downloadDocxBtn").addEventListener("click", () => {
257-
// Call validateForm() at the very beginning of the click handler
258-
if (!validateForm()) { // If validation returns false (meaning there are errors)
259-
alert("Please correct the highlighted errors in the form before downloading your resume.");
260-
return; // STOP the function execution here. This prevents the DOCX download.
261-
}
263+
if (!validateForm()) return;
262264

263-
// If validation passes (validateForm() returned true), then proceed with download
264265
const resumeContent = document.querySelector('#resume-sections').cloneNode(true);
265-
266-
// Optional: remove animations if you want cleaner output
267-
resumeContent.querySelectorAll(".fade-in, .show").forEach(el => el.classList.remove("fade-in", "show"));
268-
269-
const html = `
270-
<html xmlns:o='urn:schemas-microsoft-com:office:office'
271-
xmlns:w='urn:schemas-microsoft-com:office:word'
272-
xmlns='http://www.w3.org/TR/REC-html40'>
273-
<head><meta charset='utf-8'><title>Export HTML to Word</title></head>
274-
<body>${resumeContent.innerHTML}</body>
275-
</html>`;
266+
const preHtml = `
267+
<html>
268+
<head><meta charset='utf-8'></head>
269+
<body>`;
270+
const postHtml = "</body></html>";
271+
const html = preHtml + resumeContent.innerHTML + postHtml;
276272

277273
const blob = new Blob(['\ufeff', html], {
278274
type: 'application/msword'
279275
});
280276

281-
const url = URL.createObjectURL(blob);
282-
const link = document.createElement('a');
277+
const url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html);
278+
const link = document.createElement("a");
283279
link.href = url;
284280
link.download = 'resume.doc';
285281
document.body.appendChild(link);
286282
link.click();
287283
document.body.removeChild(link);
288284
});
289285

286+
290287
// --- DOMContentLoaded Listener for fade-in animations ---
291288
document.addEventListener("DOMContentLoaded", () => {
292289
const observer = new IntersectionObserver((entries, observer) => {

0 commit comments

Comments
 (0)