I have the following form and I’m trying to validate three cases inside affiliationCEDchecks
functions. However, the form doesn’t seem to be submiting with case 2 if
statement for some reason
<form id="orderForm" action="/mywebsite/car.htm" method="POST" onsubmit="return validateOrderForm(this) && affiliationCEDchecks()" data-gtm-form-interact-id="0">
</form>
Here is my javascript code. I’m wondering if I’m making things too complicated by returning true and false ?
function checkForCED(checkboxes) {
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
if (checkboxes[i].value === "CED") {
let partOne = document.getElementById("partOne").value;
let partTwo = document.getElementById("partTwo").value;
partOne = partOne.trim();
partTwo = partTwo.trim();
if (partOne.length == 0 && partTwo.length == 0) {
alert("Please complete the partOne or partTwo fields.");
return false;
} else {
return true;
}
}
}
}
}
function affiliationCEDchecks()
{ let returnValue = false;
//Element existence check is needed because while editing an order,
//Primary checkboxes are not present in the DOM at all
//Case 1
if (document.getElementsByName("affiliatedPrimary") !== null) {
let primaryCheckboxes = document.getElementsByName('affiliatedPrimary');
//return checkForCED(primaryCheckboxes);
if (!checkForCED(primaryCheckboxes)) {
returnValue = false;
} else {
console.log("Setting returnValue variable to true inside primary");
returnValue = true;
}
}
//Case 2
//for secondary checkboxes. No need to check for element existence as
//secondary checkboxes are always there but still checking.
if (document.getElementsByName("affiliatedSecondary") !== null) {
let secondaryCheckboxes = document.getElementsByName('affiliatedSecondary');
//return checkForCED(secondaryCheckboxes);
if (!checkForCED(secondaryCheckboxes)) {
returnValue = false;
} else {
console.log("Setting returnValue variable to true inside secondary");
returnValue = true;
}
}
//Case 3
// An additional code to look for a file code while editing an order
let doesTestBarCodeExists = document.getElementById("kitBarcodeFileCode");
console.log("Print doesTestBarCodeExists");
console.log(doesTestBarCodeExists);
console.log(doesTestBarCodeExists.value);
//check for empty string
if (doesTestBarCodeExists.value !== "") {
//check if the string equals CED
if (doesTestBarCodeExists.value === "CED") {
/* if(!altpartOneCheck()){
return false;
}
*/ let partOne = document.getElementById("partOne").value;
let partTwo = document.getElementById("partTwo").value;
partOne = partOne.trim();
partTwo = partTwo.trim();
if (partOne.length == 0 && partTwo.length == 0) {
alert("Please complete the partOne or partTwo fields. ");
returnValue = false;
} else {
console.log("Setting returnValue variable to true insideedit edit scenario version inner loop");
returnValue = true;
}
}
} else {
console.log("Setting returnValue variable to true inside edit scenario verson");
returnValue = true;
}
return returnValue;
}