25.9. Validation with JavaScript

PHOTO EMBED

Tue Aug 30 2022 03:53:33 GMT+0000 (Coordinated Universal Time)

Saved by @cruz #javascript

<script>
   window.addEventListener("load", function() {
      let form = document.querySelector("form");
      form.addEventListener("submit", function(event) {
         let usernameInput = document.querySelector("input[name=username]");
         let teamName = document.querySelector("input[name=team]");
         if (usernameInput.value === "" || teamName.value === "") {
            alert("All fields are required!");
            // stop the form submission
            event.preventDefault();
         }
      });
   });
</script>
content_copyCOPY

https://education.launchcode.org/intro-to-professional-web-dev/chapters/forms/validation-with-javascript.html