How to Create Captcha Generator in HTML
This tutorial will guide you through creating a Captcha Generator using HTML, CSS & JavaScript. Follow all the given step :
Step 1 : Create Index.html and add given html code on it.
Step 2 : Create style.css and add given css code on it.
Step 3 : Create index.js and add given JavaScript code on it.
HTML
HTML Code for Captcha Generator
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Captcha Generator</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container"><br>
<div id="captchaText"></div>
<button id="generateBtn">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-arrow-clockwise" viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M8 3a5 5 0 1 0 4.546 2.914.5.5 0 0 1 .908-.417A6 6 0 1 1 8 2z"/>
<path d="M8 4.466V.534a.25.25 0 0 1 .41-.192l2.36 1.966c.12.1.12.284 0 .384L8.41 4.658A.25.25 0 0 1 8 4.466"/>
</svg>
</button>
<input type="text" id="captchaInput" placeholder="Enter CAPTCHA">
<div id="messageContainer"></div>
<button id="submitBtn">Submit Captcha</button>
</div>
<script src="index.js"></script>
</body>
</html>
CSS
CSS Code for Captcha Generator
body{
background: #826afb;
}
.container{
margin: 200px auto;
background: #ffffff;
width: 400px;
height: 300px;
border-radius: 10px;
}
#captchaText{
width: 320px;
height: 25px;
background: #ffffff;
border: 1px solid #ccc;
margin: 24px auto;
font-size: 24px;
padding: 10px;
letter-spacing: 30px;
text-align: center;
border-radius: 5px;
}
#generateBtn{
position: relative;
left: 327px;
top: -65px;
cursor: pointer;
background: #826afb;
border: none;
height: 30px;
outline: none;
width: 35px;
border-radius: 5px;
}
svg{
filter: invert();
}
#captchaInput{
width: 320px;
position: relative;
left: -10px;
border-radius: 5px;
height: 20px;
padding: 10px;
border: 1px solid #ccc;
outline: none;
}
#submitBtn{
width: 70%;
height: 40px;
margin: 30px auto;
display: flex;
justify-content: center;
align-items: center;
background: #826afb;
border: none;
border-radius: 5px;
cursor: pointer;
color: white;
}
#messageContainer{
text-align: center;
margin-top: 20px;
}
JavaScript
JavaScript code for Captcha Generator
function generateCaptcha() {
var charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
var captchaLength = 6;
var captcha = "";
for (var i = 0; i < captchaLength; i++) {
var randomIndex = Math.floor(Math.random() * charset.length);
captcha += charset.charAt(randomIndex);
}
return captcha;
}
function displayCaptcha() {
var captchatext = document.getElementById("captchaText");
captchatext.textContent = generateCaptcha();
}
document.getElementById("generateBtn").addEventListener("click", function() {
displayCaptcha();
document.getElementById("captchaInput").value = "";
});
document.getElementById("submitBtn").addEventListener("click", function() {
var captchaInput = document.getElementById("captchaInput").value;
var captchaText = document.getElementById("captchaText").textContent;
var messageContainer = document.getElementById("messageContainer");
if (captchaInput === captchaText) {
messageContainer.textContent = "CAPTCHA verification successful!";
messageContainer.style.color = "green";
displayCaptcha();
document.getElementById("captchaInput").value = "";
} else {
messageContainer.textContent = "CAPTCHA verification failed. Please try again.";
messageContainer.style.color = "red";
displayCaptcha();
}
});
displayCaptcha();
What is HTML
HTML, or HyperText Markup Language, is the standard markup language used to create and design documents on the World Wide Web. It's essentially the backbone of web pages, providing the structure and content that web browsers interpret and display.
HTML consists of a series of elements, which are enclosed by tags to define their purpose and function on a webpage. These elements can include headings, paragraphs, images, links, lists, forms, and more. By using a combination of HTML elements, web developers can organize and present information in a structured and visually appealing manner.
HTML is not a programming language but rather a markup language. This means it doesn't have the capability to perform calculations or execute complex logic like programming languages do. Instead, HTML focuses on describing the structure and content of a document, leaving the presentation and functionality to other technologies like CSS (Cascading Style Sheets) for styling and JavaScript for interactivity.
In summary, HTML serves as the foundation for building web pages, providing the structure and content necessary for browsers to render and display information to users on the internet.
What is CSS
CSS, or Cascading Style Sheets, is a language used to style the presentation of web documents written in HTML and XML. It provides a way to control the layout, colors, fonts, and other visual aspects of a webpage, enabling developers to create attractive and responsive designs. By separating content from presentation, CSS promotes cleaner code, easier maintenance, and greater flexibility in website design. With CSS, developers can achieve consistent branding, improved user experience, and better accessibility across various devices and screen sizes.
What is JavaScript
JavaScript is a versatile programming language used for creating dynamic and interactive web content. It enables developers to manipulate HTML elements, respond to user actions, and build seamless web applications. With its extensive ecosystem of libraries and frameworks, JavaScript powers both client-side and server-side development, making it an essential tool for modern web development.