# Validated Input

> Shows a red error state only after the user has typed something invalid, with no JavaScript.

- Category: [CSS Form Elements](https://copy-css.com/category/forms)
- Tags: validation, user-invalid, error, email, input
- Live preview: https://copy-css.com/snippets/validated-input
- License: Free for personal and commercial use. No signup, no attribution required.

## HTML

```html
<label class="field-validate">
  <span>Email</span>
  <input type="email" required placeholder="you@site.dev" value="not-an-email">
  <small>Please enter a valid email address.</small>
</label>
```

## CSS

```css
.field-validate {
  display: grid;
  gap: 6px;
  width: 240px;
  font: 14px/1.3 system-ui, sans-serif;
  color: #27272a;
}

.field-validate span {
  font-weight: 600;
}

.field-validate input {
  padding: 10px 12px;
  border: 1.5px solid #d4d4d8;
  border-radius: 10px;
  background: #fff;
  font: inherit;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field-validate input:focus {
  border-color: #6366f1;
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.2);
}

.field-validate small {
  display: none;
  color: #dc2626;
}

/* Invalid, but only once the field has text in it */
.field-validate input:invalid:not(:placeholder-shown) {
  border-color: #dc2626;
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.15);
}

.field-validate input:invalid:not(:placeholder-shown) + small {
  display: block;
}
```

## Notes

- This CSS Validated Input is 41 lines of pure CSS (0.8 KB) with no JavaScript and no dependencies. It uses transitions, box shadows and CSS Grid.
- In JSX use `className` instead of `class`. In Tailwind projects put the CSS in the global stylesheet.
