Skip to content

Fixed validation logic and download behavior #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 36 additions & 39 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,71 +175,77 @@ function clearErrors() {
span.textContent = '';
});
}
function clearErrors() {
const errorFields = document.querySelectorAll(".error-message");
errorFields.forEach(field => field.textContent = "");
}

function validateForm() {
clearErrors(); // Clear any existing errors first
clearErrors();

let isValid = true; // Assume valid until an error is found
let isValid = true;
let incompleteFields = [];

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

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

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

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

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

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

return isValid;
// If all good, return true
if (isValid) return true;

// Else ask user if they want to proceed anyway
return confirm("Please correct the highlighted errors in the form before downloading your resume.");
}


// --- PDF Download Button ---

document.getElementById("downloadBtn").addEventListener("click", () => {
// Call validateForm() at the very beginning of the click handler
if (!validateForm()) { // If validation returns false (meaning there are errors)
alert("Please correct the highlighted errors in the form before downloading your resume.");
return; // STOP the function execution here. This prevents the PDF download.
}
if (!validateForm()) return;

// --- PDF Download Button ---

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

})

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

// If validation passes (validateForm() returned true), then proceed with download
const resumeContent = document.querySelector('#resume-sections').cloneNode(true);

// Optional: remove animations if you want cleaner output
resumeContent.querySelectorAll(".fade-in, .show").forEach(el => el.classList.remove("fade-in", "show"));

const html = `
<html xmlns:o='urn:schemas-microsoft-com:office:office'
xmlns:w='urn:schemas-microsoft-com:office:word'
xmlns='http://www.w3.org/TR/REC-html40'>
<head><meta charset='utf-8'><title>Export HTML to Word</title></head>
<body>${resumeContent.innerHTML}</body>
</html>`;
const preHtml = `
<html>
<head><meta charset='utf-8'></head>
<body>`;
const postHtml = "</body></html>";
const html = preHtml + resumeContent.innerHTML + postHtml;

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

const url = URL.createObjectURL(blob);
const link = document.createElement('a');
const url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html);
const link = document.createElement("a");
link.href = url;
link.download = 'resume.doc';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
});


// --- DOMContentLoaded Listener for fade-in animations ---
document.addEventListener("DOMContentLoaded", () => {
const observer = new IntersectionObserver((entries, observer) => {
Expand Down