Unexpected Form Submission Despite JavaScript Validation Errors

My JavaScript form validations are failing: even when required fields are empty or input is invalid, the form still submits. See revised snippet:

function checkFormInputs() {
  const userInput = document.myForm.primaryField.value;
  const idCode = document.myForm.codeField.value;
  if (!userInput || !idCode) {
    alert('All inputs are mandatory');
    return false;
  }
  if (isNaN(idCode) || idCode.length < 12 || idCode.length > 16) {
    alert('Enter a numeric code (12-16 digits)');
    return false;
  }
  return true;
}

hey, i had a simlar issue. check make sure your onsubmit is properly returning false so the form isn’t auto submited. sometimes the js load or binding errors can cause this. might be worth checking the script order.

Based on my experience, a common issue arises when the return value of the function is not properly handled in the event listener of the form. If the event isn’t captured correctly, returning false within the function may not have the expected effect. It would be worthwhile to check if the function is being called in the onsubmit attribute of the form, for example, using something like . Additionally, verify that no other event handlers conflict with the validation process.