Skip to content

Commit

Permalink
Add shake-invalid-input snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
Trinityyi authored Jul 31, 2022
1 parent b594c96 commit 4a087aa
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions snippets/shake-invalid-input.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
title: Shake on invalid input
tags: animation
expertise: beginner
cover: blog_images/perfect-timing.jpg
firstSeen: 2022-07-31T18:30:11+03:00
lastUpdated: 2022-07-31T18:30:11+03:00
---

Creates a shake animation on invalid input.

- Use the `pattern` attribute to define the regular expression which the input's value must match.
- Use `@keyframes` to define a shake animation, using the `margin-left` property.
- Use the `:invalid` pseudo-class to apply an `animation` to make the element shake.

```html
<input type="text" placeholder="Accept Alphabets only" pattern="[A-Za-z]*">
```

```css
@keyframes shake {
0% {margin-left: 0rem;}
25% {margin-left: 0.5rem;}
75% {margin-left: -0.5rem;}
100% {margin-left: 0rem;}
}

input:invalid
{
animation: shake 0.2s ease-in-out 0s 2;
box-shadow: 0 0 0.6em red;
}
```

0 comments on commit 4a087aa

Please sign in to comment.