Solution 1 :

You need to fix your event handlers and spelling (Elements) and form ID (registerdetails) and spelling of function names like cleanUpErrors

window.addEventListener("load", () => {
  document.getElementById("registerdetails").addEventListener("submit", event => {
    const theForm = event.target;
    let stopSubmit = false;
    cleanUpErrors();
    for (let i = 0; i < theForm.elements.length; i++) {
      if (!theForm.elements[i].checkValidity()) {
        displayInvalidities(handleInvalidities(theForm.elements[i]), theForm.elements[i].id);
        stopSubmit = true;
      }
    }
    if (stopSubmit) {
      event.preventDefault();
    }
  })
})

Solution 2 :

You are setting event listener without any function. Change in your code like this:

window.onload = () => {
    let theForm = document.getElementById("regsiterdetails");
    theForm.addEventListener("submit",(event) => {
        let stopSubmit = false;
        cleanedUpErrors();
        for (let i = 0; i < theForm.elements.length; i++) {
            if (!theForm.elements[i].checkValidity()){
                displayInvalidities(handleInvalidities(theForm.elements[i]), theForm.Elements[i].id);
                stopSubmit = true;
            }                
        }
        if (stopSubmit) {
            event.preventDefault();
        }
    }, (false);
)

}

Also check your code as there is no element with id loginform. I think you should use regsiterdetails instead of loginform.

Solution 3 :

There was no form element with the id that you’re trying to get. Try to get with the actual id which is regsiterdetails and fix your addEventListener parameter list.

function handleInvalidities(input) {
  let errMsg = " ";
  if (!input.validity.paternMismatch) {
    errMsg = "Invalid entry. Enter your details in the format shown.";
  }
  if (!input.validity.paternMismatch) {
    errMsg = "Invalid entry. This field cannot be empty. Please enter a value.";
  }
  return errMsg;
}

function displayInvalidities(errMsg, elem) {
  let elemPos = document.getElementById(elem);
  let errElem = document.createElement("span");
  errElem.setAttribute("class", "error");
  let errText = document.createTextNode(errMsg);
  errElem.appendChild(errText);
  elemPos.parentNode.insertBefore(errElem, elemPos.nextSibling);
}

function cleanUpErrors() {
  let errors = document.getElementsByClassName("error");
  for (let i = 0; i < errors.length; i++) {
    errors[i].style.display = "none";

  }

}

window.onload = () => {
  let theForm = document.getElementById("regsiterdetails");
  theForm.addEventListener("submit", (event) => {
    let stopSubmit = false;
    cleanedUpErrors();
    for (let i = 0; i < theForm.elements.length; i++) {
      if (!theForm.elements[i].checkValidity()) {
        displayInvalidities(handleInvalidities(theForm.elements[i]), theForm.Elements[i].id);
        stopSubmit = true;
      }
    }
    if (stopSubmit) {
      event.preventDefault();
    }
  }, (false));

}
<section>
  <h1>Form: validated using Javascript</h1>
  <p>Try entering the following:</p>
  <ul>
    <li>Password longer or shorter than 8 characters and/or without an uppercase, lowercase or a numeric.</li>
    <li>Passwords that do not match</li>
  </ul>
  <h2>Register</h2>
  <p>* = Required Field</p>
  <div id="formcontainer">
    <form id="regsiterdetails" action="fma_t3confirm.html">
      <div>
        <label for="username">* Userame:</label>
        <input type="text" id="username">
      </div>
      <div>
        <label for="password">* Password (Must be 8 characters and include one uppercase, one lowercase and one numeric):</label>
        <input type="password" id="password">
        <input type="checkbox" id="showpasswords">
        <label id="showpasswordslabel" for="showpasswords">Show passwords</label>
      </div>
      <div>
        <label for="retypedpassword">* Retype your password:</label>
        <input type="password" id="retypedpassword">
        <span id="passwordmatcherror"></span>
      </div>
      <div>
        <button type="submit" id="registerButton">Register</button>
      </div>
    </form>
  </div>
</section>

Problem :

I hope somebody can help me find the error in this code, i need to use simple js to create a form that only submits if there are no errors, The user name must be a valid email. The password and retyped password must be 8 characters and include one uppercase, one lowercase and one numeric. The password
and the retyped password must match. I need to make use of a regular
expression to constrain the password.
If the data rules are violated, a appropriate error messages should be displayed
and the form should be stopped from submitting.Can someone help me with why it is not doing what it is supposed to? Im still new to js and any hel would be appreciated.

function handleInvalidities(input) {
  let errMsg = " ";
  if (!input.validity.paternMismatch) {
    errMsg = "Invalid entry. Enter your details in the format shown.";
  }
  if (!input.validity.paternMismatch) {
    errMsg = "Invalid entry. This field cannot be empty. Please enter a value.";
  }
  return errMsg;
}

function displayInvalidities(errMsg, elem) {
  let elemPos = document.getElementById(elem);
  let errElem = document.createElement("span");
  errElem.setAttribute("class", "error");
  let errText = document.createTextNode(errMsg);
  errElem.appendChild(errText);
  elemPos.parentNode.insertBefore(errElem, elemPos.nextSibling);
}

function cleanUpErrors() {
  let errors = document.getElementsByClassName("error");
  for (let i = 0; i < errors.length; i++) {
    errors[i].style.display = "none";

  }

}

window.onload = () => {
  let theForm = document.getElementById("loginform");
  theForm.addEventListener("submit");
  (event) => {
    let stopSubmit = false;
    cleanedUpErrors();
    for (let i = 0; i < theForm.elements.length; i++) {
      if (!theForm.elements[i].checkValidity()) {
        displayInvalidities(handleInvalidities(theForm.elements[i]), theForm.Elements[i].id);
        stopSubmit = true;
      }
    }
    if (stopSubmit) {
      event.preventDefault();
    }
  }, (false);

}
<section>
  <h1>Form: validated using Javascript</h1>
  <p>Try entering the following:</p>
  <ul>
    <li>Password longer or shorter than 8 characters and/or without an uppercase, lowercase or a numeric.</li>
    <li>Passwords that do not match</li>
  </ul>
  <h2>Register</h2>
  <p>* = Required Field</p>
  <div id="formcontainer">
    <form id="regsiterdetails" action="fma_t3confirm.html">
      <div>
        <label for="username">* Userame:</label>
        <input type="text" id="username">
      </div>
      <div>
        <label for="password">* Password (Must be 8 characters and include one uppercase, one lowercase and one numeric):</label>
        <input type="password" id="password">
        <input type="checkbox" id="showpasswords">
        <label id="showpasswordslabel" for="showpasswords">Show passwords</label>
      </div>
      <div>
        <label for="retypedpassword">* Retype your password:</label>
        <input type="password" id="retypedpassword">
        <span id="passwordmatcherror"></span>
      </div>
      <div>
        <button type="submit" id="registerButton">Register</button>
      </div>
    </form>
  </div>
</section>

Comments

Comment posted by mplungjan

I made you a snippet. Please fix the console error

Comment posted by mplungjan

I think I covered a few more than you. Click register and it still submits

By