Table of Content
- Create Form
- Design CSS
- Validation Code
Create Form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container mt-5">
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-header wrapper">
<h3 class="title">Rays Coding :- JavaScript Validation</h3>
</div>
<div class="card-body">
<form action="" method="post">
<div class="row">
<div class="col-lg-12">
<div class="mb-3 field">
<input type="text" class="form-control" id="name" name="username" autocomplete="off">
<label for="name" class="form-label">Name</label>
</div>
</div>
</div>
<div class="row error-row" id="errorRow" style="display: none;">
<div class="col-lg-12">
<div class="mb-3 field">
<div class="alert alert-danger" id="error"></div>
</div>
</div>
</div>
<div class="row success-row" id="successRow" style="display: none;">
<div class="col-lg-12">
<div class="mb-3 field">
<div class="alert alert-success" id="success"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<div class="field">
<button type="button" class="btn btn-primary w-100" onclick="validation()">SUBMIT</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
Design CSS
@import url('https://fonts.googleapis.com/css?family=Poppins:400,500,600,700&display=swap');
*
{
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
.card
{
width: 60%;
margin: 10px auto;
}
.wrapper
{
background: #fff;
border-radius: 15px;
box-shadow: 0px 15px 20px rgba(0,0,0,0.1);
background: linear-gradient(-135deg, #c850c0, #4158d0);
}
.title
{
color: #fff;
text-shadow: 0 0 10px #fff;
text-align: center;
}
.field
{
height: 50px;
width: 100%;
margin-top: 20px;
position: relative;
}
.field input.form-control
{
height: 100%;
width: 100%;
box-shadow: none;
outline: none;
font-size: 17px;
padding-left: 20px;
border: 1px solid lightgrey;
border-radius: 25px;
transition: all 0.3s ease;
}
.field input.form-control:focus,
.field input.form-control:valid
{
border-color: #4158d0;
}
.field label
{
position: absolute;
top: 50%;
left: 20px;
color: #999999;
font-weight: 400;
font-size: 17px;
pointer-events: none;
transform: translateY(-50%);
transition: all 0.3s ease;
}
.field input.form-control:focus ~ label,
.field input.form-control:valid ~ label
{
top: 0%;
font-size: 16px;
color: #4158d0;
background: #fff;
transform: translateY(-50%);
}
.field button
{
border: none;
outline: none;
background: linear-gradient(-135deg, #c850c0, #4158d0);
}
.error-row
{
display: none;
}
Validation Code
function validation()
{
// get the input box value
let inputVal = document.getElementById('name').value.trim();
// Regex value
let nameRegex = /^[a-zA-Z\s]+$/;
// Conditions
if (inputVal === '')
{
document.getElementById('error').innerText = "Please enter the name.";
document.getElementById('errorRow').style.display = "block";
hideError();
return false;
}
else if (inputVal.length < 3)
{
document.getElementById('error').innerText = "Please enter a valid name with at least 3 characters.";
document.getElementById('errorRow').style.display = "block";
hideError();
return false;
}
else if (!nameRegex.test(inputVal))
{
document.getElementById('error').innerText = "Please enter a valid name with only alphabetic characters.";
document.getElementById('errorRow').style.display = "block";
hideError();
return false;
}
else
{
document.getElementById('success').innerText = 'You Enter ' + inputVal;
document.getElementById('successRow').style.display = "block";
return true;
}
}
// Hide error message
function hideError()
{
setTimeout(function() {
document.getElementById('errorRow').style.display = "none";
}, 5000);
}