Skip to content

Instantly share code, notes, and snippets.

@amit08255
Created January 13, 2023 11:06
Show Gist options
  • Save amit08255/e8171ffefc150292c60f2629c43087f3 to your computer and use it in GitHub Desktop.
Save amit08255/e8171ffefc150292c60f2629c43087f3 to your computer and use it in GitHub Desktop.
HTML5 Form Validation Tricks

HTML5 Form Validation Tricks

Disable submit button if any input HTML5 validations failed

form.validated-forms:invalid {
  button[type="submit"]:not(:disabled) {
    opacity: 0.5;
    pointer-events: none;
  }
}

Check if input HTML5 validations passed on change

inputElement.checkValidity() returns true when HTML5 validations passed.

<!DOCTYPE html>
<html>
  <head>
    <title>Hello, World!</title>
  </head>
  <body>
      <form>
        <input type="email" onchange="myFunction(this)">
      </form>

<script>
function myFunction(e) {
  console.log(e.checkValidity());
}
</script>
  </body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment