HTML 5 Email Input Validation

HTML 5 Email Input Validation

HTML

<div class="container">
     <div class="formContainer">
       <form action="#">
         <div class="emailInput">
           <input
             type="email"
             name="email"
             id="email"
             placeholder="Email"
             required
           />
           <i class="material-icons icon email">email</i>
           <i class="material-icons icon success">done</i>
           <i class="material-icons icon error">report_problem</i>
         </div>
       </form>
     </div>
   </div>

SCSS

@import url('https://fonts.googleapis.com/css?family=Roboto');
$fontFamily:'Roboto', sans-serif;
@function rem($value) {
  @return unquote(($value/1px)/16+"rem");
}
$white:#ffffff;
$success:#33d353;
$error:#FE2946;
$initial:#0071FE;
$background:#efefef;
*{
  font-family:$fontFamily;
}
body{
  background-color: $background;
}
.container {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  display: flex;
  justify-content: center;
  align-items: center;
}
*{
  box-sizing:border-box;
}
.formContainer{
  position:relative;
  display: flex;
  form{
    display: inline-flex;
    flex-direction:column;
    .emailInput{
      position:relative;
      display: inline-flex;
      align-items: center;
      border-radius:rem(6px);
      overflow:hidden;
      box-shadow:0px 2px 20px rgba(0,0,0,0.25);
      input{
        border:none;
        padding:rem(10px);
        box-shadow:none;
        outline:0;
        background-color: $white;
        font-size:rem(16px);
        padding:rem(20px);
        padding-right:rem(60px);
        border-radius:rem(6px);
        border:rem(4px) solid $initial;
        color:#212121;
        width: rem(400px);
        transition:all ease-in-out 0.2s;
        & ~ .icon{
          position:absolute;
          top: 0;
          bottom: 0;
          right: 0;
          margin:auto;
          display: inline-flex;
          justify-content: center;
          align-items: center;
          padding:rem(4px);
          opacity:0;
          transition:all ease-in-out 0.2s;
          width:rem(55px);
          background-color: red;
          color:$white;
          &.email{
            opacity:1;
            background-color: $initial;
          }
          &.success{
            background-color: $success;
          }
          &.error{
            background-color: $error;
          }
        } 
      }
    }
    
    /*blur states*/
    .emailInput{
      input.blurred{
        &:valid{
          border-color:$success;
          & ~ .icon{
            opacity:0;
            &.success{
              opacity:1;
            }
          }
        }
        
        &:invalid{
          border-color:$error;
          & ~ .icon{
            opacity:0;
            &.error{
              opacity:1;
            }
          }
        }        
      }
    }
  }
}

CSS

@import url("https://fonts.googleapis.com/css?family=Roboto");
@import url("https://fonts.googleapis.com/css?family=Courgette");
* {
  font-family: "Roboto", sans-serif;
  box-sizing: border-box;
}

body {
  background-color: #efefef;
}

/***********************/
body {
  padding-top: 60px;
}
body .subscribe {
  position: fixed;
  top: 10px;
  right: 10px;
  text-align: center;
  z-index: 5000;
  border-radius: 4px;
  border: 1px solid #ffffff;
}
body .subscribe a {
  display: inline-block;
  border-radius: inherit;
  padding: 10px 20px;
  background-color: #ff0000;
  color: #ffffff;
  text-decoration: none;
  font-family: "Courgette", cursive;
}
body .subscribe a:hover {
  background-color: #cc0000;
}
body .subscribe a span {
  margin-left: 2px;
}

/********************/
.container {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  display: flex;
  justify-content: center;
  align-items: center;
}

.formContainer {
  position: relative;
  display: flex;
}

.formContainer form {
  display: inline-flex;
  flex-direction: column;
}

.formContainer form .emailInput {
  position: relative;
  display: inline-flex;
  align-items: center;
  border-radius: 0.375rem;
  overflow: hidden;
  box-shadow: 0px 2px 20px rgba(0, 0, 0, 0.25);
}

.formContainer form .emailInput input {
  border: none;
  padding: 1.25rem;
  padding-right: 3.75rem;
  border: 0.25rem solid #0071fe;
  outline: 0;
  background-color: #ffffff;
  font-size: 1rem;
  border-radius: 0.375rem;
  color: #212121;
  width: 25rem;
  transition: all ease-in-out 0.2s;
}

.formContainer form .emailInput input ~ .icon {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  margin: auto;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  padding: 0.25rem;
  width: 3.4375rem;
  opacity: 0;
  color: #ffffff;
  transition: all ease-in-out 0.2s;
}

.formContainer form .emailInput input ~ .icon.email {
  opacity: 1;
  background-color: #0071fe;
}

.formContainer form .emailInput input ~ .icon.success {
  background-color: #33d353;
}

.formContainer form .emailInput input ~ .icon.error {
  background-color: #fe2946;
}

/** blur states*/
.emailInput input.blurred:valid {
  border-color: #33d353;
}

.emailInput input.blurred:valid ~ .icon {
  opacity: 0;
}

.emailInput input.blurred:valid ~ .icon.success {
  opacity: 1;
}

.emailInput input.blurred:invalid {
  border-color: #fe2946;
}

.emailInput input.blurred:invalid ~ .icon {
  opacity: 0;
}

.emailInput input.blurred:invalid ~ .icon.error {
  opacity: 1;
}

JAVASCRIPT

$(document).ready(function() {
  $(".emailInput").each(function(index, emailInput) {
    var inputElement = $(emailInput).find("input");

    $(inputElement)
      .on("focus", function() {
        $(this).removeClass("blurred");
      })
      .on("blur", function() {
        $(this).addClass("blurred");
      });
  });
});

 

Add a Comment

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