Tell us what’s happening:
"I’m working on a form validation function, and while most of the tests are passing, a few are failing. Specifically, I’m having trouble with
Updating border colors correctly for #complaints-group, #complaint-description, #solutions-group, and #solution-description when they become valid or invalid.
Handling radio buttons properly for #solutions-group—the validation doesn’t seem to update the border color correctly when a radio button is selected.
Ensuring my isValid function correctly checks
Your code so far
const form = document.getElementById('form');
const fullName = document.getElementById("full-name");
const email = document.getElementById("email");
const orderNo = document.getElementById("order-no");
const productCode = document.getElementById("product-code");
const quantity = document.getElementById("quantity");
const complaintDescription = document.getElementById("complaint-description");
const solutionDescription = document.getElementById("solution-description");
const complaintsGroup = document.getElementById("complaints-group");
const solutionsGroup = document.getElementById("solutions-group");
const otherComplaint = document.getElementById("other-complaint");
const otherSolution = document.getElementById("other-solution");
form.addEventListener("submit", function (event) {
event.preventDefault();
const validationResults = validateForm();
console.log(validationResults);
console.log("Is Form Valid?", isValid(validationResults));
});
function validateForm() {
const fullNameValue = fullName.value.trim();
const emailValue = email.value.trim();
const orderNoValue = orderNo.value.trim();
const productCodeValue = productCode.value.trim();
const quantityValue = parseInt(quantity.value.trim(), 10);
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const orderNoPattern = /^2024\d{6}$/;
const productCodePattern = /^[A-Za-z]{2}\d{2}-[A-Za-z]\d{3}-[A-Za-z]{2}\d$/;
const complaintCheckboxes = document.querySelectorAll('input[name="complaint"]:checked');
const isComplaintSelected = complaintCheckboxes.length > 0;
const solutionRadios = document.querySelectorAll('input[name="solution"]:checked');
const isSolutionSelected = solutionRadios.length > 0;
const isOtherComplaintValid = !otherComplaint.checked || (otherComplaint.checked && complaintDescription.value.trim().length >= 20);
const isOtherSolutionValid = !otherSolution.checked || (otherSolution.checked && solutionDescription.value.trim().length >= 20);
return {
"full-name": fullNameValue !== "",
"email": emailPattern.test(emailValue),
"order-no": orderNoPattern.test(orderNoValue),
"product-code": productCodePattern.test(productCodeValue),
"quantity": !isNaN(quantityValue) && quantityValue > 0,
"complaints-group": isComplaintSelected,
"complaint-description": isOtherComplaintValid,
"solutions-group": isSolutionSelected,
"solution-description": isOtherSolutionValid,
};
}
function isValid(validationResults) {
return Object.values(validationResults).every(result => result === true);
}
function updateBorderColor(element, isValid) {
element.style.borderColor = isValid ? "green" : "red";
}
fullName.addEventListener("change", function () {
updateBorderColor(fullName, fullName.value.trim() !== "");
});
email.addEventListener("change", function () {
updateBorderColor(email, /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email.value.trim()));
});
orderNo.addEventListener("change", function () {
updateBorderColor(orderNo, /^2024\d{6}$/.test(orderNo.value.trim()));
});
productCode.addEventListener("change", function () {
updateBorderColor(productCode, /^[A-Za-z]{2}\d{2}-[A-Za-z]\d{3}-[A-Za-z]{2}\d$/.test(productCode.value.trim()));
});
quantity.addEventListener("change", function () {
const quantityValue = parseInt(quantity.value.trim(), 10);
updateBorderColor(quantity, !isNaN(quantityValue) && quantityValue > 0);
});
const complaintCheckboxes = document.querySelectorAll('#complaints-group input[type="checkbox"]');
function updateComplaintsGroupBorder() {
const isAnyChecked = [...complaintCheckboxes].some(checkbox => checkbox.checked);
complaintsGroup.style.border = "2px solid " + (isAnyChecked ? "green" : "red");
}
complaintCheckboxes.forEach(checkbox => {
checkbox.addEventListener("change", updateComplaintsGroupBorder);
});
updateComplaintsGroupBorder();
otherComplaint.addEventListener("change", function () {
updateBorderColor(complaintDescription, !otherComplaint.checked || complaintDescription.value.trim().length >= 20);
});
complaintDescription.addEventListener("input", function () {
if (otherComplaint.checked) {
updateBorderColor(complaintDescription, complaintDescription.value.trim().length >= 20);
} else {
complaintDescription.style.borderColor = "";
}
});
const solutionRadios = document.querySelectorAll('#solutions-group input[type="radio"]');
function updateSolutionsGroupBorder() {
const isAnyChecked = Array.from(solutionRadios).some(radio => radio.checked);
updateBorderColor(solutionsGroup, isAnyChecked);
}
solutionRadios.forEach(radio => {
radio.addEventListener("change", updateSolutionsGroupBorder);
});
otherSolution.addEventListener("change", function () {
updateBorderColor(solutionDescription, !otherSolution.checked || solutionDescription.value.trim().length >= 20);
});
solutionDescription.addEventListener("input", function () {
if (otherSolution.checked) {
updateBorderColor(solutionDescription, solutionDescription.value.trim().length >= 20);
} else {
solutionDescription.style.borderColor = "";
}
});