DEV Community

Enes Kılıç
Enes Kılıç

Posted on

6 2

Form Validation with CSS

In this post, I will show you how to validation the required form fields with CSS.

If you want to see full version of this project or all HTML-CSS projects look this Repository


Create HTML

  <form>

    <!--==== Add required attribute to input tag  ====-->
    <input type="password" required />

    <button type="submit"> Login </button>

  </form>
Enter fullscreen mode Exit fullscreen mode

CSS

form:invalid button {
  pointer-events: none;
}

form:valid button {
  pointer-events: initial;
}
Enter fullscreen mode Exit fullscreen mode

My output with some styles

Top comments (3)

Collapse
 
alvaromontoro profile image
Alvaro Montoro

pointer-events work nicely when there's a pointer/mouse... But keyboard users will love to have a word about this solution 😋

Collapse
 
iainsimmons profile image
Iain Simmons

Not exactly the same use case.

You can't do a basic "repeat password" validation with just HTML5 attributes.

But yeah, definitely agree that in many cases, people are too eager to reach for a JS library when things can be done natively in HTML and styled in CSS.

Collapse
 
eboye profile image
eboye

Yup, discovered that few years ago. It's amazing feature. Great share!

And btw, it has amazing support: caniuse.com/form-validation

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay