Sling Academy
Home/Tailwind CSS/Form Validation with Tailwind CSS (without Javascript)

Form Validation with Tailwind CSS (without Javascript)

Last updated: December 18, 2023

In Tailwind CSS, we can validate a form without writing any Javascript code. The main point here is to add the peer utility class to the input field we want to validate and then use the peer-invalid modifier to show an error message when the entered value is invalid. Words might be confusing and hard to understand. The complete example below will give a better understanding.

Example Preview

This example creates a minimal contact form. The requirements are:

  • The name must not be empty
  • The email address must be a valid email address
  • The message must be at least one character

When a field does not satisfy the requirements, a corresponding error message will appear right below it. Here’s the demo:

The Code

<body class="w-screen h-screen bg-purple-700 flex justify-center items-center">
    <form class="p-10 bg-white rounded-lg drop-shadow-lg space-y-4" action="">
        <h1 class="text-xl font-light">Contact KindaCode.com</h1>

        <!-- Name -->
        <div class="flex flex-col">
            <label for="name">Name</label>
            <input type="text" name="name" id="name" required class="peer border border-slate-400">

            <p class="invisible peer-invalid:visible text-red-700 font-light">
                Please enter your name
            </p>
        </div>

        <!-- Email -->
        <div class="flex flex-col">
            <label for="email">Email</label>
            <input type="email" name="email" id="email" required class="peer border border-slate-400">
            <p class="invisible peer-invalid:visible text-red-700 font-light">
                Please enter a valid email address
            </p>
        </div>

        <!-- Message -->
        <div class="flex flex-col">
            <label for="message">Message</label>
            <textarea name="message" id="message" cols="30" rows="3" required
                class="peer border border-slate-400"></textarea>
            <p class="invisible peer-invalid:visible text-red-700 font-light">
                This field cannot be empty
            </p>
        </div>
        <button type="submit" class="px-5 py-1 bg-amber-500">Send</button>
    </form>
</body>

That’s if. Happy coding & have a nice experience with Tailwind CSS!

Previous Article: How to Create Pill Buttons with Tailwind CSS

Series: Styling Form Elements with Tailwind CSS

Tailwind CSS

Related Articles

You May Also Like

  • How to Create Striped Tables with Tailwind CSS
  • Tailwind CSS: Creating Shimmer Loading Placeholder (Skeleton)
  • Tailwind CSS: How to Create Parallax Scrolling Effect
  • How to Style Lists in Tailwind CSS
  • Text Underline in Tailwind CSS
  • How to Change Mouse Cursor in Tailwind CSS
  • first-of-type and last-of-type in Tailwind CSS
  • Tailwind CSS: Create a User Card with Circle Avatar
  • Tailwind CSS: How to Create Columns with the Same Height
  • How to Style Placeholder Text with Tailwind CSS
  • Tailwind CSS: Make a Div 100% Height & Width of the Viewport
  • Using :not() selector in Tailwind CSS
  • Tailwind CSS: How to Create a Stepper
  • Using ::before and ::after in Tailwind CSS
  • Tailwind CSS: How to Create a Vertical Divider Line
  • How to Create Pill Buttons with Tailwind CSS
  • Tailwind CSS: How to Disable Text Selection
  • Tailwind CSS: How to Disable Resizing of Textarea
  • Tailwind CSS: Expand a Text Input on Focus (without Javascript)