Thumbnail for form validation episode
3 minutes

Mastering Validation Magic: A CSS Pseudo-class Extravaganza for Forms

Greetings, fellow wizards of the web! Today, we’re delving into the magical world of CSS pseudo-classes tailored for form validation. Imagine adding a dash of enchantment to your forms, guiding users through a captivating experience. Get ready to wield the power of pseudo-classes to conjure stylish validations!

:valid and :invalid - The Gatekeepers of Truth:

Meet the dynamic duo—:valid and :invalid pseudo-classes, the gatekeepers of form validation. They allow you to style form elements based on their validity state. Imagine a world where your inputs turn green when they’re validated and red when they’re not:

input:valid {
  border: 2px solid #2ecc71; /* Valid inputs get a green border */
}

input:invalid {
  border: 2px solid #e74c3c; /* Invalid inputs wear a fiery red border */
}

With this magic, users will instantly know if their entries are worthy or if a spell has gone awry. You can try these on a form with required inputs, email input, number inputs with min and/or max attributes, etc.

:required and :optional - The Destiny Deciders:

Enter the realm of destiny with the :required and :optional pseudo-classes. These selectors allow you to style required and optional form elements differently, guiding users through the fields they must conquer:

input:required {
  background-color: #ecf0f1; /* Required fields get a serene background color */
}

input:optional {
  background-color: #ffffff; /* Optional fields are a blank canvas */
}

Users will feel the cosmic guidance as they navigate through the necessary and optional sections of your enchanted forms.

:placeholder-shown - The Chameleon Whisperer:

Introducing the :placeholder-shown pseudo-class—the chameleon whisperer. It allows you to style elements based on whether their placeholders are visible, meaning whether or not the input is empty or not:

input:placeholder-shown {
  border: lightblue solid 2px; /* Empty inputs with placeholder
                                  will show a nice border */
}

This subtle touch ensures that inputs with placeholders gracefully manifest themselves, giving users a clear view of inputs they may have missed.

:out-of-range and :in-range - The Boundary Guardians:

Meet the boundary guardians—:out-of-range and :in-range pseudo-classes. They empower you to style elements based on whether their values fall within or outside specified ranges. Picture number or dates inputs that change color when they step out of bound. You will have to define a min and/or max attributes for these inputs:

input:in-range {
  background-color: #3498db; /* Background color for in-range inputs */
}

input:out-of-range {
  background-color: #e74c3c; /* Background color for out-of-range inputs */
}

Users will feel the power of the elements as they navigate through the magical values of your enchanted inputs.

:read-only and :read-write - The Chronicles of Editability:

Enter the chronicles of editability with :read-only and :read-write pseudo-classes. Style read-only and read-write inputs differently, creating a visual hierarchy that guides users through the realms of interaction:

input:read-only {
  background-color: #f0f0f0; /* Read-only inputs get a muted background */
}

input:read-write {
  background-color: #ffffff; /* Read-write inputs are a pristine canvas */
}

Users will sense the sacredness of read-only fields and the boundless potential of read-write fields in your mystical forms. Read only elements might disabled or readonly inputs/textarea (via attribute) or an input of type “button” for example. Simply put, a readonly element is not editable by the user.

Congratulations, fellow enchanters! You’ve now mastered the art of using CSS pseudo-classes for form validation. With these spells in your repertoire, users will embark on an interactive journey through your forms, guided by the subtle yet powerful cues you’ve woven into the very fabric of your web enchantments. Until our paths cross again, may your validations be seamless and your web spells ever magical! ✨🔮

Photo of Joel Filipe on Unsplash