Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to create RGB color generator using HTML CSS and JavaScript ?
Next article icon

Captcha Generator using HTML CSS and JavaScript

Last Updated : 28 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A captcha is a way of identifying a user whether the user is human or not.  A captcha is made up with the help of combining letters and digits. it ensures that the user who is trying to access the platform is a human. So without wasting the time let's get started.

Application of Captcha

  • Form Authentication: For login and sign up it can be used to ensure that an end user is human.
  • Preventing Fake Registrations: With the captcha, we can prevent bots from creating an account on a system.  
  • Preventing Fake comments:  This way bot would not be able to Comment on a system,
  • NetBanking and financial institutes: To ensure that Authentication is only done by humans and this way manipulation of transactions can be prevented.

Note: Captcha can protect From some Automated attacks as well.

Approach

  • HTML Structure: Set up the HTML elements including an input field for the CAPTCHA code, a refresh icon for regenerating the CAPTCHA, a display area for the CAPTCHA, and a submit button for validation.
  • CSS Styling: Apply styles to the CAPTCHA display area, input field, and submit button to control layout, appearance, and behavior (e.g., shadows, borders, font styles).
  • CAPTCHA Generation: Implement a generate() function in JavaScript that creates a random 5-character CAPTCHA string from alphanumeric characters and displays it in the designated area when the page loads or when the refresh icon is clicked.
  • CAPTCHA Validation: Implement a printmsg() function that compares the user-entered CAPTCHA code with the generated one, providing feedback ("Matched" or "not Matched") and regenerating the CAPTCHA after each attempt.
  • User Interaction: Allow users to refresh the CAPTCHA and submit their input for validation, with immediate feedback displayed based on whether the input matches the generated CAPTCHA.cop

Example: The below code will generate a design for a captcha and from the CSS file we will add style and on the image(refresh) click,  we will generate a new captcha by calling generate() method from JavaScript.

HTML
<!DOCTYPE html> <html lang="en">    <head>     <meta charset="UTF-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport"            content="width=device-width, initial-scale=1.0">     <link rel="stylesheet"      href="captcha.css">     <link rel="stylesheet" href= "https://use.fontawesome.com/releases/v5.15.3/css/all.css"         integrity= "sha384-SZXxX4whJ79/gErwcOYf+zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk"         crossorigin="anonymous">     <script src="captcha.js"></script> </head>  <body onload="generate()">     <div id="user-input" class="inline">         <input type="text"                 id="submit"                 placeholder="Captcha code" />     </div>      <div class="inline" onclick="generate()">         <i class="fas fa-sync"></i>     </div>      <div id="image"           class="inline"           selectable="False">     </div>     <input type="submit"             id="btn"             onclick="printmsg()" />      <p id="key"></p> </body>    </html> 
CSS
#image{     margin-top: 10px;     box-shadow: 5px 5px 5px 5px gray;     width: 60px;;     padding: 20px;     font-weight: 400;     padding-bottom: 0px;     height: 40px;     user-select: none;     text-decoration:line-through;     font-style: italic;     font-size: x-large;     border: red 2px solid;     margin-left: 10px;      } #user-input{     box-shadow: 5px 5px 5px 5px gray;     width:auto;     margin-right: 10px;     padding: 10px;     padding-bottom: 0px;     height: 40px;     border: red 0px solid; } input{     border:1px black solid; } .inline{     display:inline-block; } #btn{     box-shadow: 5px 5px 5px grey;     color: aqua;     margin: 10px;     background-color: brown; } 
JavaScript
let captcha; function generate() {      // Clear old input     document.getElementById("submit").value = "";      // Access the element to store     // the generated captcha     captcha = document.getElementById("image");     let uniquechar = "";      const randomchar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";      // Generate captcha for length of     // 5 with random character     for (let i = 1; i < 5; i++) {         uniquechar += randomchar.charAt(             Math.random() * randomchar.length)     }      // Store generated input     captcha.innerHTML = uniquechar; }  function printmsg() {     const usr_input = document         .getElementById("submit").value;      // Check whether the input is equal     // to generated captcha or not     if (usr_input == captcha.innerHTML) {         let s = document.getElementById("key")             .innerHTML = "Matched";         generate();     }     else {         let s = document.getElementById("key")             .innerHTML = "not Matched";         generate();     } } 

Output:

Captcha-Generator
Captcha Generator

Next Article
How to create RGB color generator using HTML CSS and JavaScript ?

J

jeetpurohit989
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • TrueGeek-2021
  • JavaScript-Projects

Similar Reads

  • How to create a Color Generator using HTML CSS and JavaScript ?
    In this article, we will develop a Color Generator application using HTML, CSS, and JavaScript. In this application, we have created the Multi Format color generator. Users can select Colour Types like RGB, HEX, and CMYK. Use the sliders to customize the color and Inout field in case of HEX and colo
    6 min read
  • Create an QR Code Generator Project using HTML CSS & JavaScript
    In today's digital world, QR codes are widely used and provide a practical way to share information and access content with a quick scan. This deeply manually operated will show you step-by-step how to create a QR code generator from scratch using HTML, CSS, and JavaScript. This article will give yo
    3 min read
  • Random Quote Generator Using HTML, CSS and JavaScript
    A Random Quote Generator is capable of pulling quotes randomly from an API, a database, or simply from an array. We will be designing a Random Quote Generator from scratch using HTML, CSS, JavaScript, and type.fit API. The webpage displays a random quote from a collection and upon the click of a but
    8 min read
  • How to create Hex color generator using HTML CSS and JavaScript?
    Hex color codes are six-digit combinations representing the amount of red, green, and blue (RGB) in a color. These codes are essential in web design and development for specifying colors in HTML and CSS. The hex color generator provides the hex code for any selected color, making it a valuable tool
    2 min read
  • How to create RGB color generator using HTML CSS and JavaScript ?
    In this article, we will create a RGB color generator using HTML, CSS, and JavaScript. Using RGB color generator, we can construct all the colors from the combination of Red, Green, Blue colors. Each color is represented by the range of decimal numbers from 0 to 255 (256 levels for each color). So,
    3 min read
  • Design Random Color Generator using HTML CSS and JavaScript
    A Random Color Generator app is used to create random colors with a simple button click, displaying both HEX and RGB codes that users can copy. It's built using HTML, CSS, and JavaScript, making it a handy tool for designers or developers who need quick color ideas. The app can also include a Dark M
    3 min read
  • Number Sequences Generator using HTML CSS and JavaScript
    In this article, we are going to implement a number sequence generator based on HTML, CSS, and JavaScript. In this program, we have three different types of sequences: the Fibonacci Series, the Prime Number Series, and the Even Odd Number Series. Preview of final output: Let us have a look at how th
    5 min read
  • Create GeeksforGeeks Clone Using HTML CSS & JavaScript
    In this article, we'll make a clone of the GeeksforGeeks website. The clone includes home, about, courses, and contact pages. On the home page, we've added sections like categories, features, testimonials, and FAQs, similar to the GFG website. We named our clone GeeksforGeeks 2.0 to avoid copyright
    15+ min read
  • Build a Password Generator App with HTML CSS and JavaScript
    In this article, we will build a password generator application using HTML, CSS, and JavaScript. This application will generate strong and secure passwords based on user preferences, such as password length and character types. It aims to provide a convenient tool for users to generate random passwo
    3 min read
  • Create a Simon Game using HTML CSS & JavaScript
    In this article, we will see how to create a Simon Game using HTML, CSS, and JavaScript. In a Simon game, if the player succeeds, the series becomes progressively longer and more complex. Once the user is unable to repeat the designated order of the series at any point, the game is over. Prerequisit
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences