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>JavaScript Validation</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">
<label class="form-label">Language</label>
<div class="row">
<div class="col-lg-6">
<input class="language" type="checkbox" value="hindi">
<span>Hindi</span>
</div>
<div class="col-lg-6">
<input class="language" type="checkbox" value="english">
<span>English</span>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<input class="language" type="checkbox" value="tamil">
<span>Tamil</span>
</div>
<div class="col-lg-6">
<input class="language" type="checkbox" value="marathi">
<span>Marathi</span>
</div>
</div>
</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>
2. CSS Styling
@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 input[type="checkbox"]
{
border: 1px solid #4158d0;
border-radius: 4px;
width: 16px;
height: 16px;
background-color: #ffffff;
cursor: pointer;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
display: inline-block;
position: relative;
}
.field input[type="checkbox"]:checked
{
background-color: #4158d0;
border-color: #4158d0;
}
.field input[type="checkbox"]:checked::after
{
content: '';
position: absolute;
top: 2px;
left: 6px;
width: 4px;
height: 8px;
border: solid white;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
}
.field input[type="checkbox"]:hover
{
border-color: #1e3a8a;
}
.field input[type="checkbox"]:focus
{
outline: none;
box-shadow: 0 0 2px 2px rgba(65, 88, 208, 0.5);
}
.field span
{
color: #4158d0;
}
.field button
{
border: none;
outline: none;
background: linear-gradient(-135deg, #c850c0, #4158d0);
}
.error-row
{
display: none;
}
3. JavaScript Validation Script
function validation()
{
let checkboxes = document.querySelectorAll('.language');
let checkedLanguages = [];
checkboxes.forEach(function(checkbox)
{
if (checkbox.checked)
{
checkedLanguages.push(checkbox.value.toUpperCase());
}
});
if (checkedLanguages.length === 0)
{
document.getElementById('error').innerText = "Please select at least one language.";
document.getElementById('errorRow').style.display = "block";
hideError();
return false;
}
else
{
document.getElementById('success').innerText = 'Selected Languages: ' + checkedLanguages.join(', ');
document.getElementById('successRow').style.display = "block";
return true;
}
}
// Hide error message
function hideError() {
setTimeout(function() {
document.getElementById('errorRow').style.display = "none";
}, 5000);
}
Validation
FunctionThis function is triggered when the submit button on the form is clicked. It performs validation on the language checkboxes.
1. Selecting Checkboxes:
let checkboxes = document.querySelectorAll('.language');
language
(which are the checkboxes for language selection). let checkedLanguages = [];
checkboxes.forEach(function(checkbox)
{
if (checkbox.checked)
{
checkedLanguages.push(checkbox.value.toUpperCase());
}
});
if (checkedLanguages.length === 0)
{
document.getElementById('error').innerText = "Please select at least one language.";
document.getElementById('errorRow').style.display = "block";
hideError();
return false;
}
checkedLanguages.length === 0
). If true, it displays an error message:- Sets the inner text of the error element to "Please select at least one language."
- Makes the errorRow (which contains the error message) visible by setting its display style to "block".
- Calls the hideError() function to hide the error message after a delay.
- Returns false to prevent form submission.
else
{
document.getElementById('success').innerText = 'Selected Languages: ' + checkedLanguages.join(', ');
document.getElementById('successRow').style.display = "block";
return true;
}
hideError
Function function hideError() {
setTimeout(function() {
document.getElementById('errorRow').style.display = "none";
}, 5000);
}