Today I Learned

hashrocket A Hashrocket project

Check if a form is valid

You can check if a form passes HTML validations with javascript using the checkValidity function:

<form name="login">
   <input type="email" name="email" required />

   <input type="password" name="password" required />

 <div style="margin-top: 1rem">
   <input type="submit" value="login" />
   <button type="button" id="validate">

  const button = document.querySelector("#validate")
  if (button) {
    button.addEventListener("click", function() {
      alert(document.forms.login.checkValidity() ? "Form is valid" : "INVALID");
See More #javascript TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.