Tell us what’s happening:
My code is working as intended and requested, but still getting failed tasks:
Failed: 24. When all of the checkboxes from #complaints-group
are changed to the unchecked state, you should set #complaints-group
’s border color to red
.
Failed: 30. Your isValid
function should take the validateForm()
as its argument and return true
when all the form fields have been filled correctly.
Failed: 32. You should call isValid
when you try to submit the form.
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<title>Customer Complaint Form</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Complaint Form</h1>
<form id="form">
<fieldset id="personal-info">
<div>
<label for="full-name">Full Name:</label>
<input type="text" id="full-name" name="full-name" placeholder="John Doe">
</div>
<div>
<label for="email">Email Address:</label>
<input type="email" id="email" name="email" placeholder="example@domain.com">
</div>
</fieldset>
<hr>
<fieldset id="product-info">
<div>
<label for="order-no">Order No:</label>
<input type="text" id="order-no" name="order-no" placeholder="2024######">
</div>
<div>
<label for="product-code">Product Code:</label>
<input type="text" id="product-code" name="product-code" placeholder="XX##-X###-XX#">
</div>
<div>
<label for="quantity">Quantity:</label>
<input type="number" id="quantity" name="quantity" min="1">
</div>
</fieldset>
<fieldset id="complaints-group">
<legend>Complaint Reason:</legend>
<div>
<input type="checkbox" id="damaged-product" name="complaint" value="damaged-product">
<label for="damaged-product">Damaged Product</label>
</div>
<div>
<input type="checkbox" id="nonconforming-product" name="complaint" value="nonconforming-product">
<label for="nonconforming-product">Nonconforming Product</label>
</div>
<div>
<input type="checkbox" id="delayed-dispatch" name="complaint" value="delayed-dispatch">
<label for="delayed-dispatch">Delayed Dispatch</label>
</div>
<div>
<input type="checkbox" id="other-complaint" name="complaint" value="other">
<label for="other-complaint">Other</label>
</div>
</fieldset>
<div id="complaint-description-container">
<legend>Description of Complaint Reason</legend>
<textarea placeholder="Describe the reason of your complaint in at least 20 characters"
name="complaint-textarea" id="complaint-description"></textarea>
</div>
<fieldset id="solutions-group">
<legend>Desired Solution</legend>
<input type="radio" name="solutions" id="refund" value="refund">
<label for="refund">Refund</label>
<input type="radio" name="solutions" id="exchange" value="exchange">
<label for="exchange">Exchange</label>
<input type="radio" name="solutions" id="other-solution" value="other">
<label for="other-solution">Other</label>
</fieldset>
<div id="solution-description-container">
<legend>Description of Desired Solution</legend>
<textarea placeholder="Describe the desired solution to your issue in at least 20 characters"
name="solution-textarea" id="solution-description"></textarea>
</div>
<div id="btn-container">
<button type="submit" id="submit-btn">Submit</button>
<span id="message-box"></span>
</div>
</form>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
* {
box-sizing: border-box;
}
h1 {
text-align: center;
}
#form {
max-width: 70%;
margin: auto;
border-radius: 10px;
box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
padding: 10px;
}
input {
border-color: rgb(118, 118, 118);
}
#personal-info input, #product-info input {
width: 100%;
margin-bottom: 10px;
}
fieldset {
margin-bottom: 10px;
border-radius: 5px;
border-color: rgb(118, 118, 118);
}
textarea {
width: 100%;
border-color: rgb(118, 118, 118);
}
#btn-container {
display: flex;
justify-content: space-between;
align-items: center;
}
#submit-btn, #clear-btn {
margin: 10px 15px 0;
}
/* file: script.js */
// Variables
const form = document.getElementById("form")
const fullName = document.getElementById("full-name");
const emailAddress = document.getElementById("email");
const orderNumber = document.getElementById("order-no");
const productCode = document.getElementById("product-code");
const productQuantity = document.getElementById("quantity");
const complaintReason = document.querySelectorAll("input[type=checkbox]")
const complaintDescription = document.getElementById("complaint-description");
const desiredSolution = document.querySelectorAll("input[type=radio]");
const solutionDescription = document.getElementById("solution-description");
const submitButton = document.getElementById("submit-btn");
const messageBox = document.getElementById("message-box");
// Validate Functions
function validateFullName(fullName) {
return fullName.value != "";
}
function validateEmail(emailAddress) {
const emailRegex = /^\S+\@\S+\.\S+$/i;
return emailRegex.test(emailAddress.value)
}
function validateOrderNumber(orderNumber) {
const orderNumRegex = /^2024\d{6}$/;
return orderNumRegex.test(orderNumber.value);
}
function validateProductCode(productCode) {
const productCodeRegex = /^[a-zA-Z]{2}\d{2}\-[a-zA-Z]{1}\d{3}\-[a-zA-Z]{2}\d{1}$/;
return productCodeRegex.test(productCode.value)
}
function validateQuantity(quantity) {
const quantityRegex = /^[1-9]+$/;
return quantityRegex.test(quantity.value)
}
function validateComplaintsGroup(checkedReasons) {
return checkedReasons.length !== 0;
}
function validateComplaintDescription(checkedReasons) {
const length = complaintDescription.value.length;
if (checkedReasons.includes("other")) {
return length >= 20;
}
return length === 0;
}
function validateDesiredSolution(desiredSolution) {
return desiredSolution.length == 1
}
function validateSolutionDescription(checkedSolutions) {
if (checkedSolutions.includes("other")) {
return solutionDescription.value.length >= 20;
}
return solutionDescription.value.length == 0;
}
// Get Value Functions
function getCheckedReason() {
return Array.from(complaintReason)
.filter(i => i.checked)
.map(i => i.value);
}
function getDesiredSolution() {
return Array.from(desiredSolution)
.filter(i => i.checked)
.map(i => i.value);
}
// Form
function validateForm() {
let filledForm = {
'full-name': validateFullName(fullName),
'email': validateEmail(emailAddress),
'order-no': validateOrderNumber(orderNumber),
'product-code': validateProductCode(productCode),
'quantity': validateQuantity(productQuantity),
'complaints-group': validateComplaintsGroup(getCheckedReason()),
'complaint-description': validateComplaintDescription(getCheckedReason()),
'solutions-group': validateDesiredSolution(getDesiredSolution()),
'solution-description': validateSolutionDescription(getDesiredSolution())
}
return filledForm
}
function isValid(validatedForm) {
const arrayForm = Object.values(validatedForm)
return arrayForm.every((val) => val == true)
}
// Change event
form.addEventListener("change", (event) => {
var borderColor = validateForm()[event.target.id]? "green" : "red";
event.target.style.borderColor = borderColor
if(event.target.type == "checkbox" || event.target.type == "radio"){
borderColor = validateForm()["complaints-group"]? "green" : "red"
event.target.closest("fieldset").style.borderColor = borderColor
}
})
// Helper function to set border color based on validation
function setBorderColor(field, isValid) {
const borderColor = isValid ? "green" : "red";
if (NodeList.prototype.isPrototypeOf(field)) {
field.forEach((element) => {
element.closest("fieldset").style.borderColor = borderColor;
});
} else {
field.style.borderColor = borderColor;
}
}
// SUBMIT BUTTON
submitButton.addEventListener("click", (e) => {
e.preventDefault();
const form = validateForm();
if(!isValid(form)){
setBorderColor(fullName, form['full-name']);
setBorderColor(emailAddress, form['email']);
setBorderColor(orderNumber, form['order-no']);
setBorderColor(productCode, form['product-code']);
setBorderColor(productQuantity, form['quantity']);
setBorderColor(complaintReason, form['complaints-group']);
setBorderColor(complaintDescription, form['complaint-description']);
setBorderColor(desiredSolution, form['solutions-group']);
setBorderColor(solutionDescription, form['solution-description']);
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:138.0) Gecko/20100101 Firefox/138.0
Challenge Information:
Build a Customer Complaint Form - Build a Customer Complaint Form