Hello developers, today in this blog, you will learn how to create Login Form Validation using HTML, CSS & JavaScript.
A Form Validation is a technical process where a web form checks if the information provided by a user is correct. The form will either alert the user by showing the error message, or the form will be validated and the user will be able to continue with their login process. Form Validation means to check that the user's entered credentials - Email and Password are valid and correct or not. The user will not get access to the restricted page until the user entered a valid email and password.
In this blog (Login Form Validation), as you see on the webpage, there is a login form that contains Email, Password and, the Login Button. At first, the errors are not shown but when the user clicks on the login button without entering Email and Password then the errors will appear. JavaScript code is used to validate the form. The email and password must be entered based on the condition else an error will be shown. If the field is not filled correctly, the icon with red color border and an error text will appear with a shake effect.
The source code of this Login Form Validation using HTML, CSS & JavaScript is given below, if you want the source code of this program, you can copy it. You can use this Login Form Validation code with your creativity and can take this form to the next level.
Login Form Validation using HTML, CSS & JavaScript [Source Code]
To make this website, you have to make three files: an HTML file, a CSS file & a JavaScript file. First, create an HTML file with the name of index.html and remember, you have to create a file with a .html 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: 100%;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #00C9A7;
}
::selection{
color: #fff;
background: #00C9A7;
}
.wrapper{
width: 380px;
padding: 40px 30px 50px 30px;
background: #12192c;
border-radius: 5px;
text-align: center;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
}
.wrapper header{
font-size: 35px;
font-weight: 600;
color: #fff;
}
.wrapper form{
margin: 40px 0;
}
form .field{
width: 100%;
margin-bottom: 20px;
}
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;
}
}
form .field .input-area{
height: 50px;
width: 100%;
position: relative;
}
form input{
width: 100%;
height: 100%;
outline: none;
padding: 0 45px;
font-size: 18px;
background: none;
color: #e1d9ec;
border-radius: 5px;
border: 1px solid #e1d9ec;
border-bottom-width: 2px;
transition: all 0.2s ease;
}
form .field input:focus,
form .field.valid input{
border-color: #00C9A7;
}
form .field.shake input,
form .field.error input{
border-color: #dc3545;
}
.field .input-area i{
position: absolute;
top: 50%;
font-size: 18px;
pointer-events: none;
transform: translateY(-50%);
}
.input-area .icon{
left: 15px;
color: #bfbfbf;
transition: color 0.2s ease;
}
.input-area .error-icon{
right: 15px;
color: #dc3545;
}
form input:focus ~ .icon,
form .field.valid .icon{
color: #00C9A7;
}
form .field.shake input:focus ~ .icon,
form .field.error input:focus ~ .icon{
color: #bfbfbf;
}
form input::placeholder{
color: #bfbfbf;
font-size: 17px;
}
form .field .error-txt{
color: #dc3545;
text-align: left;
margin-top: 5px;
}
form .field .error{
display: none;
}
form .field.shake .error,
form .field.error .error{
display: block;
}
form .pass-txt{
text-align: left;
margin-top: -10px;
}
.wrapper a{
color: #00C9A7;
text-decoration: none;
}
.wrapper a:hover{
text-decoration: underline;
}
form input[type="submit"]{
height: 50px;
margin-top: 30px;
color: #fff;
padding: 0;
border: none;
background: #00C9A7;
cursor: pointer;
border-bottom: 2px solid rgba(0,0,0,0.1);
transition: all 0.3s ease;
}
form input[type="submit"]:hover{
background: #22E4AC;
}
.sign-txt{
color: #fff;
}
.credit{
margin-top: 20px;
text-align: center;
color: #000;
font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif;
}
.credit a{
text-decoration: none;
color:#000;
font-weight: bold;
}
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") : checkPass();
setTimeout(()=>{
eField.classList.remove("shake");
pField.classList.remove("shake");
}, 500);
eInput.onkeyup = ()=>{checkEmail();}
pInput.onkeyup = ()=>{checkPass();}
function checkEmail(){
let pattern = /^[^ ]+@[^ ]+\.[a-z]{2,3}$/;
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 checkPass(){
let pattern = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$/;
if(!pInput.value.match(pattern)){
pField.classList.add("error");
pField.classList.remove("valid");
let errorTxt = pField.querySelector(".error-txt");
(pInput.value != "") ? errorTxt.innerText = "must contain 8 characters, atleast one number, one uppercase letter and one lowercase letter" : errorTxt.innerText = "Password can't be blank";
}else{
pField.classList.remove("error");
pField.classList.add("valid");
}
}
if(!eField.classList.contains("error") && !pField.classList.contains("error")){
window.location.href = form.getAttribute("action");
}
}
Thank you for reading our blog. If you face any problem in creating this Login Form Validation using HTML, CSS & JavaScript then contact us or comment us. We’ll try to provide a solution to your problem as soon as possible.
Post a Comment
Thank you
Learning robo team