-
Notifications
You must be signed in to change notification settings - Fork 17
Enhance CAPTCHA Validator: Mobile Responsive, Modular JS, and Accessibility Improvements #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e735b01
Update script.js(Refactored with generateCaptcha() function)
AbhinashRao 116a811
Update index.html(Accessibility + UX Enhancements.)
AbhinashRao 0ba6a71
Update script.js
AbhinashRao ba5d4ea
Update script.js
AbhinashRao 9745cc9
Update styles.css(Mobile Responsiveness & UI Enhancements)
AbhinashRao 7b5ccbd
Update index.html(Accessibility + UX Enhancements.)
AbhinashRao 918e8b6
Update README.md
AbhinashRao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
55 changes: 53 additions & 2 deletions
README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,53 @@ | ||
| # CAPTCHA-Validator | ||
| This project demonstrates how you can implement CAPTCHA Validation in your website using HTML, CSS and JavaScript. | ||
| # CAPTCHA Validator π | ||
|
|
||
| A lightweight and responsive CAPTCHA validation project built with **HTML**, **CSS**, and **JavaScript**. This project generates a random CAPTCHA string, validates user input, and provides real-time feedback β helping protect your forms from bots. | ||
|
|
||
| --- | ||
|
|
||
| ## β¨ Features | ||
|
|
||
| - π¨ Custom CAPTCHA canvas rendering | ||
| - π CAPTCHA refresh functionality | ||
| - π§ Real-time input validation (on button click or Enter key) | ||
| - π± Fully responsive design (mobile/tablet/desktop) | ||
| - π― Clean and modern UI with Roboto fonts | ||
| - β Clear feedback for correct/incorrect inputs | ||
| --- | ||
|
|
||
| ## π How to Run | ||
|
|
||
| 1. **Clone the Repository** | ||
|
|
||
| git clone https://github.com/your-username/captcha-validator.git | ||
| cd captcha-validator | ||
| Open in Browser | ||
|
|
||
| Just open index.html in any modern browser. | ||
|
|
||
| π§© Project Structure | ||
|
|
||
| π captcha-validator/ | ||
| β | ||
| βββ index.html # Main HTML page | ||
| βββ styles.css # Styling with responsive media queries | ||
| βββ script.js # CAPTCHA logic and interaction | ||
| βββ README.md # Project documentation | ||
|
|
||
| π± Responsive Design | ||
| Mobile-first approach | ||
| Fluid resizing for tablets and desktops | ||
| Buttons and input boxes adjust automatically | ||
|
|
||
| π‘ Use Cases | ||
| Login or Sign-Up forms | ||
| Contact pages | ||
| Basic form submissions | ||
|
|
||
| π οΈ Tech Stack | ||
| HTML5 | ||
| CSS3 | ||
| JavaScript | ||
|
|
||
| π Author | ||
| Abhinash Rao | ||
| GitHub | LinkedIn |
31 changes: 21 additions & 10 deletions
index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 83 additions & 52 deletions
script.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,76 +1,107 @@ | ||
|
|
||
| // document.querySelector() is used to select an element from the document using its ID | ||
| let captchaText = document.getElementById('captcha'); | ||
| var ctx = captchaText.getContext("2d"); | ||
| let ctx = captchaText.getContext("2d"); | ||
| ctx.font = "30px Roboto"; | ||
| ctx.fillStyle = "#08e5ff"; | ||
|
|
||
|
|
||
| let userText = document.getElementById('textBox'); | ||
| let submitButton = document.getElementById('submitButton'); | ||
| let output = document.getElementById('output'); | ||
| let refreshButton = document.getElementById('refreshButton'); | ||
|
|
||
| let alphaNums = [ | ||
| 'A', 'B', 'C', 'D', 'E', 'F', 'G', | ||
| 'H', 'I', 'J', 'K', 'L', 'M', 'N', | ||
| 'O', 'P', 'Q', 'R', 'S', 'T', 'U', | ||
| 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', | ||
| 'c', 'd', 'e', 'f', 'g', 'h', 'i', | ||
| 'j', 'k', 'l', 'm', 'n', 'o', 'p', | ||
| 'q', 'r', 's', 't', 'u', 'v', 'w', | ||
| 'x', 'y', 'z', '0', '1', '2', '3', | ||
| '4', '5', '6', '7', '8', '9' | ||
| ]; | ||
|
|
||
| let c = ""; | ||
|
|
||
| function generateCaptcha() { | ||
| userText.value = ""; | ||
| output.innerHTML = ""; | ||
| ctx.clearRect(0, 0, captchaText.width, captchaText.height); | ||
|
|
||
| let captchaArray = []; | ||
| for (let i = 0; i < 7; i++) { | ||
| captchaArray.push(alphaNums[Math.floor(Math.random() * alphaNums.length)]); | ||
| } | ||
|
|
||
| // alphaNums contains the characters with which you want to create the CAPTCHA | ||
| let alphaNums = ['A', 'B', 'C', 'D', 'E', 'F', 'G', | ||
| 'H', 'I', 'J', 'K', 'L', 'M', 'N', | ||
| 'O', 'P', 'Q', 'R', 'S', 'T', 'U', | ||
| 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', | ||
| 'c', 'd', 'e', 'f', 'g', 'h', 'i', | ||
| 'j', 'k', 'l', 'm', 'n', 'o', 'p', | ||
| 'q', 'r', 's', 't', 'u', 'v', 'w', | ||
| 'x', 'y', 'z', '0', '1', '2', '3', | ||
| '4', '5', '6', '7', '8', '9']; | ||
| c = captchaArray.join(''); | ||
| ctx.fillText(c, captchaText.width / 4, captchaText.height / 2); | ||
|
|
||
| // Add random lines | ||
| for (let i = 0; i < 5; i++) { | ||
| ctx.beginPath(); | ||
| ctx.moveTo(Math.random() * captchaText.width, Math.random() * captchaText.height); | ||
| ctx.lineTo(Math.random() * captchaText.width, Math.random() * captchaText.height); | ||
| ctx.strokeStyle = getRandomColor(); | ||
| ctx.lineWidth = 1; | ||
| ctx.stroke(); | ||
| } | ||
|
|
||
| // This loop generates a random string of 7 characters using alphaNums | ||
| // Further this string is displayed as a CAPTCHA | ||
| let emptyArr = []; | ||
| for (let i = 1; i <= 7; i++) { | ||
| emptyArr.push(alphaNums[Math.floor(Math.random() * alphaNums.length)]); | ||
| // Add random dots | ||
| for (let i = 0; i < 30; i++) { | ||
| ctx.beginPath(); | ||
| ctx.arc(Math.random() * captchaText.width, Math.random() * captchaText.height, 1.5, 0, 2 * Math.PI); | ||
| ctx.fillStyle = getRandomColor(); | ||
| ctx.fill(); | ||
| } | ||
| } | ||
|
|
||
| function getRandomColor() { | ||
| const letters = '0123456789ABCDEF'; | ||
| let color = '#'; | ||
| for (let i = 0; i < 6; i++) { | ||
| color += letters[Math.floor(Math.random() * 16)]; | ||
| } | ||
| return color; | ||
| } | ||
| var c = emptyArr.join(''); | ||
| ctx.fillText(emptyArr.join(''), captchaText.width/4, captchaText.height/2); | ||
|
|
||
| // This event listener is stimulated whenever the user press the "Enter" button | ||
| // "Correct!" or "Incorrect, please try again!" message is | ||
| // displayed after validating the input text with CAPTCHA | ||
| userText.addEventListener('keyup', function(e) { | ||
| // Initial CAPTCHA render | ||
| generateCaptcha(); | ||
|
|
||
| // Event: Press Enter | ||
| userText.addEventListener('keyup', function (e) { | ||
| if (e.key === 'Enter') { | ||
| if (userText.value === c) { | ||
| output.classList.add("correctCaptcha"); | ||
| output.innerHTML = "Correct!"; | ||
| } else { | ||
| output.classList.add("incorrectCaptcha"); | ||
| output.innerHTML = "Incorrect, please try again!"; | ||
| } | ||
| checkCaptcha(); | ||
| } | ||
| }); | ||
|
|
||
| // This event listener is stimulated whenever the user clicks the "Submit" button | ||
| // "Correct!" or "Incorrect, please try again!" message is | ||
| // displayed after validating the input text with CAPTCHA | ||
| submitButton.addEventListener('click', function() { | ||
| // Event: Submit | ||
| submitButton.addEventListener('click', function () { | ||
| checkCaptcha(); | ||
| }); | ||
|
|
||
| // Event: Refresh | ||
| refreshButton.addEventListener('click', function () { | ||
| generateCaptcha(); | ||
| }); | ||
|
|
||
| // Function to validate CAPTCHA | ||
| function checkCaptcha() { | ||
| if (userText.value === c) { | ||
| output.classList.remove("incorrectCaptcha"); | ||
| output.classList.add("correctCaptcha"); | ||
| output.innerHTML = "Correct!"; | ||
| output.innerHTML = "β Correct!"; | ||
|
|
||
| userText.disabled = true; | ||
| submitButton.disabled = true; | ||
| refreshButton.disabled = true; | ||
|
|
||
| setTimeout(() => { | ||
| alert("CAPTCHA verified! Proceeding..."); | ||
| // window.location.href = "success.html"; // Uncomment to redirect | ||
| }, 2000); | ||
| } else { | ||
| output.classList.remove("correctCaptcha"); | ||
| output.classList.add("incorrectCaptcha"); | ||
| output.innerHTML = "Incorrect, please try again!"; | ||
| output.innerHTML = "β Incorrect, please try again!"; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // This event listener is stimulated whenever the user press the "Refresh" button | ||
| // A new random CAPTCHA is generated and displayed after the user clicks the "Refresh" button | ||
| refreshButton.addEventListener('click', function() { | ||
| userText.value = ""; | ||
| let refreshArr = []; | ||
| for (let j = 1; j <= 7; j++) { | ||
| refreshArr.push(alphaNums[Math.floor(Math.random() * alphaNums.length)]); | ||
| } | ||
| ctx.clearRect(0, 0, captchaText.width, captchaText.height); | ||
| c = refreshArr.join(''); | ||
| ctx.fillText(refreshArr.join(''),captchaText.width/4, captchaText.height/2); | ||
| output.innerHTML = ""; | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.