Login Form Validation With Shake Effect

Login Form Validation With Shake Effect

Creating a login form with shake effect using HTML, CSS, and JavaScript is a simple yet effective way to enhance the user experience of your website. Here is a general outline of how you can achieve this:

  1. Start by creating the HTML structure of your login form. This should include the form element, a username field, a password field, and a submit button.
  2. Use CSS to style the form to your liking, including adding any desired visual effects such as hover states for buttons or focus states for form fields.
  3. Next, add some JavaScript to validate the form fields when the submit button is clicked. This could include checking that the username and password fields are not empty, that the password meets certain length or complexity requirements, etc.
  4. To create the shake effect, you will need to add a small amount of JavaScript that animates the form element when an error is detected. One way to do this is by adding a class to the form element that adds a shaking animation, and then triggering this class to be added or removed as needed.
  5. Finally, make sure to test your login form thoroughly to ensure it is functioning correctly and the shake effect looks and feels good to the user.

Video Tutorial Of How To Create Login Form Validation With Shake Effect

As you have seen on the given video tutorial of How to create Login Form validation with Shake Effect,

If you are feeling bored watching the given video tutorial of How to create Login Form validation with Shake Effect then you can copy or download the given codes below:

How to create Login Form validation with Shake Effect | Free Source Code

To create this program (Login Form validation with Shake Effect). First, you need to create three Files one HTML File and one is CSS File and another one is JavaScript File. After creating these files just paste the following codes in your file.

In the first place, make a HTML document with the name of index.html and paste the given codes in your HTML record. Keep in mind, you’ve to make a document with .html extension.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Login Form validation with Shake Effect | S-Tech04</title>
  <link rel="stylesheet" href="style.css">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css"/>
</head>
<body>
  <div class="wrapper">
    <div class="logo"></div>
    <div class="title">Login Form</div>
    <form action="https://www.youtube.com/c/STech04" method="post">
      <div class="field email">
        <div class="input-area">
          <input type="text" placeholder="Email Adderss">
          <i class="icon fas fa-envelope"></i>
          <i class="error error-icon fas fa-exclamation-circle"></i>
        </div>
        <div class="error error-txt">Email can't be blank</div>
      </div>
      <div class="field password">
        <div class="input-area">
          <input type="password" placeholder="Enter password">
          <i class="icon fas fa-lock"></i>
          <i class="error error-icon fas fa-exclamation-circle"></i>
        </div>
        <div class="error error-txt">Password can't be blank</div>
      </div>
      <input type="submit" value="Login">
    </form>
    <div class="link">
      <a href="#">Forget password?</a> or <a href="#">sign up</a>
    </div>
  </div>

  <script src="./script.js"></script>
</body>
</html>

Second, make a CSS record with the name of style.css and glue the given codes in your CSS document. Keep in mind, you’ve to make a record with .css extension.

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600&display=swap');
*{
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body
{
  width: 1005;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #ecf0f3;
}
.wrapper
{
  width: 380px;
  padding: 40px 30px 50px 30px;
  background: #ecf0f3;
  box-shadow: 13px 13px 20px #cbced1,
  -13px -13px 20px #ffffff;
  border-radius: 40px;
}
.wrapper .logo
{
  width: 100px;
  height: 100px;
  background: url(./logo.png);
  background-size: cover;
  border-radius: 50%;
  box-shadow: 0 0 2px #5f5f5f,
  0 0 0 5px #ecf0f3,
  8px 8px 15px #a7aaaf;
  margin: 0 auto;
}
.wrapper .title
{
  padding-top: 24px;
  font-size: 24px;
  letter-spacing: 0.5px;
  text-align: center;
}
.wrapper form
{
  margin: 20px 0;
  margin-bottom: 0;
}
.wrapper form .field
{
  width: 100%;
  margin-bottom: 30px;
}
form .field.shake
{
  animation: shake 0.3s ease-in-out;
}

@keyframes shake
{
  0%, 100%
  {
    margin-left: 0px;
  }
  20%, 80%
  {
    margin-left: -12px;
  }
  40%, 60%
  {
    margin-left: 12px;
  }
}
.field .input-area
{
  height: 50px;
  width: 100%;
  position: relative;
  border-radius: 25px;
  box-shadow: inset 8px 8px 8px #cbced1,
  inset -8px -8px 8px #fff;
}
.field .input-area input
{
  width: 100%;
  height: 100%;
  background: none;
  border-radius: 5px;
  border: none;
  outline: none;
  caret-color: #24cfaa;
  padding: 0px 40px;
  font-size: 18px;
}
.input-area i
{
  position: absolute;
  top: 50%;
  pointer-events: none;
  font-size: 18px;
  transform: translateY(-50%);
}
.input-area i.icon
{
  left: 15px;
  color: #bfbfbf;
  transition: color 0.2s ease;
}
.input-area .error-icon
{
  right: 15px;
  color: #dc3545;
}
.input-area input:focus ~ .icon,
form .field.valid .icon
{
  color: #24cfaa;
}
form .field.shake input:focus ~ .icon,
form .field.error input:focus ~ .icon
{
  color: #bfbfbf;
}

input::placeholder
{
  color: #bfbfbf;
  font-size: 17px;
}

.field .error-txt
{
  color: #dc3545;
  margin-top: 5px;
  margin-left: 15px;
}

form .field .error
{
  display: none;
}
form .field.shake .error,
form .field.error .error
{
  display: block;
}

form input[type="submit"]
{
  width: 100%;
  height: 60px;
  text-align: center;
  color: #fff;
  background: #24cfaa;
  border-radius: 30px;
  outline: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  box-shadow: 3px 3px 8px #d1d1d1,
  -3px -3px 8px #ffffff;
}
form input[type="submit"]:hover
{
  background: #18bb98;
}

.wrapper .link
{
  padding-top: 20px;
  text-align: center;
}
.link a
{
  text-decoration: none;
  color: #949393;
  font-size: 15px;
}
.link a:hover
{
  text-decoration: underline;
}

Last, create a JavaScript file with the name of script.js and paste the given codes in your JavaScript file. Remember, you’ve to create a file with .js extension.

const form = document.querySelector('form');
eField = form.querySelector('.email'),
eInput = eField.querySelector('input'),
pField = form.querySelector('.password'),
pInput = pField.querySelector('input');


form.onsubmit = (e)=>{
    e.preventDefault();

    (eInput.value == "") ? eField.classList.add("shake", "error") : checkEmail();
    (pInput.value == "") ? pField.classList.add("shake", "error") : checkPassword();

    setTimeout(() => {
        eField.classList.remove('shake');
        pField.classList.remove('shake');
    }, 500);

    eInput.onkeyup = ()=>{checkEmail();}
    pInput.onkeyup = ()=>{checkPassword();}

    function checkEmail(){
        let pattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/; //pattern for validate email
        if(!eInput.value.match(pattern)){
            eField.classList.add("error");
            eField.classList.remove("valid");
            let errorTxt = eField.querySelector('.error-txt');
            (eInput.value != "") ? errorTxt.innerText = "Enter a valid email address" : errorTxt.innerText = "Email can't be blank"
        }else{
            eField.classList.remove("error");
            eField.classList.add("valid");
        }
    }

    function checkPassword(){
        if(pInput.value == ""){
            pField.classList.add('error');
            pField.classList.remove('valid');
        }else{
            pField.classList.remove('error');
            pField.classList.add('valid');
        }
    }

    if(!eField.classList.contains('error') && !pField.classList.contains('error')){
        window.location.href = form.getAttribute('action') //redirecting user to the specified url which is inside action attribute of form tag
    }
}

You have successfully created a login form with a shake effect for validation. If you have any issues or errors, you can download the source code files for free. Just click the download button and extract the downloaded .zip file.

Download Now
Please Wait...
Direct Link Click Here
Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *