HTML
<!--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">
<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>CSS
*,
*: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;
}
JS
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';
}
}
/* All the credits: https://www.youtube.com/watch?v=1tnINeBLNj4*/
Deja tu comentario