비전공자 개발일기

Create Email Validation 본문

Javascript

Create Email Validation

HiroDaegu 2022. 12. 8. 00:37
728x90
SMALL

<html lang="ko">

<head>
    <title>Email Validation</title>
    <!--FontAwesome Icons-->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css">
    <!--Google Fonts-->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500&display=swap" rel="stylesheet">
    <!--Stylesheet-->
    <link rel="stylesheet" href="style.css">
    <!--Script-->
    <script defer src="main.js"></script>
</head>

<body>
    <div class="container">
        <label for="email-id">Email Id</label><br>
        <input type="email" id="email-id" oninput="checker()">
        <div id="icon">

        </div>
        <p id="error-msg">Please Enter A Valid Email Id</p>
    </div>

</body>

</html>
*,
*:before,
*:after{
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}
body{
    height: 100vh;
    background: linear-gradient(
        135deg,
        #61d954,
        #2ebf75
    );
}
.container{
    width: 400px;
    background-color: #ffffff;
    padding: 50px 30px;
    position: absolute;
    transform: translate(-50%,-50%);
    top: 50%;
    left: 50%;
    border-radius: 10px;
    box-shadow: 25px 25px 30px rgba(0,0,0,0.15);
    color: #111111;
}
label,
input,
#error-msg{
    font-family: 'Poppins',sans-serif;
}
label{
    display: inline-block;
    font-weight: 500;
    margin-bottom: 10px;
}
input[type="email"]{
    display: inline-block;
    border: 2px solid #d1d3d4;
    width: 88%;
    height: 50px;
    border-radius: 5px;
    outline: none;
    letter-spacing: 0.5px;
    font-weight: 400;
}
#icon{
    float: right;
    height: 50px;
    position: relative;
    font-size: 25px;
    padding-top: 10px;
}
#error-msg{
    display: none;
    color: #ff2851;
    font-size: 14px;
    margin-top: 10px;
}
// Regex Pattern: /^[a-zA-Z][a-zA-Z0-9\-\_\.]+@[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}$/

let emailId = document.getElementById("email-id");
let errorMsg = document.getElementById("error-msg");
let icon = document.getElementById("icon");
let mailRegex = /^[a-zA-Z][a-zA-Z0-9\-\_\.]+@[a-zA-Z0-9]{2,}\.[a-zA-Z0-9]{2,}$/;

function checker() {
  icon.style.display = "inline-block";
  if (emailId.value.match(mailRegex)) {
    icon.innerHTML = '<i class="fas fa-check-circle"></i>';
    icon.style.color = "#2ecc71";
    errorMsg.style.display = "none";
    emailId.style.border = "2px solid #2ecc71";
  } else if (emailId.value == "") {
    icon.style.display = "none";
    errorMsg.style.display = "none";
    emailId.style.border = "2px solid #d1d3d4";
  } else {
    icon.innerHTML = '<i class="fas fa-exclamation-circle"></i>';
    icon.style.color = "#ff2851";
    errorMsg.style.display = "block";
    emailId.style.border = "2px solid #ff2851";
  }
}
728x90
LIST

'Javascript' 카테고리의 다른 글

Get User Location  (0) 2022.12.11
Calculator basic operator  (0) 2022.12.09
Password Strength Checker with color  (0) 2022.12.07
Image Distribution Effect  (0) 2022.12.06
Product Filter  (0) 2022.12.04