Introduction
In modern web applications, validating user input in real-time is a key aspect of user experience. For developers working with digital systems, communication protocols, or binary data, ensuring that users enter valid bits (0s and 1s) is crucial.
In this tutorial, we will create a Real-Time Bit Input Validator using HTML, CSS, and JavaScript, which checks for valid binary input and even-length sequences instantly without needing to submit a form.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real-Time Input Bit Validator</title>
<style>
body{
font-family: Arial, sans-serif;
background:#f4f6f9;
margin:40px;
}
.container{
max-width:600px;
margin:auto;
background:white;
padding:30px;
border-radius:8px;
box-shadow:0 3px 12px rgba(0,0,0,0.1);
}
h1{ color:#333; }
input{
width:100%;
padding:12px;
border:1px solid #ccc;
border-radius:5px;
margin-top:10px;
}
.valid{ border:2px solid #2ecc71; }
.invalid{ border:2px solid #e74c3c; }
.message{
margin-top:12px;
font-weight:bold;
}
.success{ color:#2ecc71; }
.error{ color:#e74c3c; }
</style>
</head>
<body>
<div class="container">
<h1>Real-Time Bit Input Checker</h1>
<p>Enter bits separated by commas (example: <strong>1,0,1,1</strong>)</p>
<input type="text" id="inputBits" placeholder="Type bits here">
<div id="message" class="message"></div>
</div>
<script>
const inputField = document.getElementById("inputBits");
const message = document.getElementById("message");
inputField.addEventListener("input", function(){
const input = inputField.value.trim();
if(input === ""){
message.textContent = "";
inputField.classList.remove("valid","invalid");
return;
}
const bitsArray = input.split(',').map(bit => bit.trim());
const validBits = bitsArray.every(bit => bit === "0" || bit === "1");
if(!validBits){
message.textContent = "Only 0 and 1 are allowed.";
message.className = "message error";
inputField.classList.add("invalid");
inputField.classList.remove("valid");
return;
}
if(bitsArray.length % 2 === 0){
message.textContent = "✓ Valid input: Even number of bits.";
message.className = "message success";
inputField.classList.add("valid");
inputField.classList.remove("invalid");
}
else{
message.textContent = "✗ The number of bits must be even.";
message.className = "message error";
inputField.classList.add("invalid");
inputField.classList.remove("valid");
}
});
</script>
</body>
</html>
Adding Input Event Listeners to All Input Fields for Real-Time Validation
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real-Time Input Bit Validator</title>
<style>
body{
font-family: Arial, sans-serif;
background:#f4f6f9;
margin:40px;
}
.container{
max-width:600px;
margin:auto;
background:white;
padding:30px;
border-radius:8px;
box-shadow:0 3px 12px rgba(0,0,0,0.1);
}
h1{ color:#333; }
input{
width:100%;
padding:12px;
border:1px solid #ccc;
border-radius:5px;
margin-top:10px;
}
.valid{ border:2px solid #2ecc71; }
.invalid{ border:2px solid #e74c3c; }
.message{
margin-top:12px;
font-weight:bold;
}
.success{ color:#2ecc71; }
.error{ color:#e74c3c; }
</style>
</head>
<body>
<div class="container">
<h1>Real-Time Bit Input Checker</h1>
<p>Enter bits separated by commas (example: <strong>1,0,1,1</strong>)</p>
<input type="text" id="inputBits" placeholder="Type bits here">
<input type="text" id="inputBits1" placeholder="Type bits here">
<div id="message" class="message"></div>
</div>
<script>
const inputFields = document.querySelectorAll('input');
const message = document.getElementById("message");
inputFields.forEach(field => {
field.addEventListener("input", function(){
const value = field.value.trim(); // Correctly get value of this input
if(value === ""){
message.textContent = "";
field.classList.remove("valid","invalid");
return;
}
const bitsArray = value.split(',').map(bit => bit.trim());
const validBits = bitsArray.every(bit => bit === "0" || bit === "1");
if(!validBits){
message.textContent = "Only 0 and 1 are allowed.";
message.className = "message error";
field.classList.add("invalid");
field.classList.remove("valid");
return;
}
if(bitsArray.length % 2 === 0){
message.textContent = "✓ Valid input: Even number of bits.";
message.className = "message success";
field.classList.add("valid");
field.classList.remove("invalid");
}
else{
message.textContent = "✗ The number of bits must be even.";
message.className = "message error";
field.classList.add("invalid");
field.classList.remove("valid");
}
});
});
</script>
</body>
</html>