|
| 1 | +// document.querySelector() is used to select an element from the document using its ID |
| 2 | +let captchaText = document.querySelector('#captcha'); |
| 3 | +let userText = document.querySelector('#textBox'); |
| 4 | +let submitButton = document.querySelector('#submitButton'); |
| 5 | +let output = document.querySelector('#output'); |
| 6 | +let refreshButton = document.querySelector('#refreshButton'); |
| 7 | + |
| 8 | + |
| 9 | +// alphaNums contains the characters with which you want to create the CAPTCHA |
| 10 | +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']; |
| 11 | +let emptyArr = []; |
| 12 | + |
| 13 | +// This loop generates a random string of 7 characters using alphaNums |
| 14 | +// Further this string is displayed as a CAPTCHA |
| 15 | +for (let i = 1; i <= 7; i++) { |
| 16 | + emptyArr.push(alphaNums[Math.floor(Math.random() * alphaNums.length)]); |
| 17 | +} |
| 18 | +captchaText.innerHTML = emptyArr.join(''); |
| 19 | + |
| 20 | + |
| 21 | +// This event listener is stimulated whenever the user press the "Enter" button |
| 22 | +// "Correct!" or "Incorrect, please try again" message is |
| 23 | +// displayed after validating the input text with CAPTCHA |
| 24 | +userText.addEventListener('keyup', function(e) { |
| 25 | + // Key Code Value of "Enter" Button is 13 |
| 26 | + if (e.keyCode === 13) { |
| 27 | + if (userText.value === captchaText.innerHTML) { |
| 28 | + output.classList.add("correctCaptcha"); |
| 29 | + output.innerHTML = "Correct!"; |
| 30 | + } else { |
| 31 | + output.classList.add("incorrectCaptcha"); |
| 32 | + output.innerHTML = "Incorrect, please try again"; |
| 33 | + } |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +// This event listener is stimulated whenever the user clicks the "Submit" button |
| 38 | +// "Correct!" or "Incorrect, please try again" message is |
| 39 | +// displayed after validating the input text with CAPTCHA |
| 40 | +submitButton.addEventListener('click', function() { |
| 41 | + if (userText.value === captchaText.innerHTML) { |
| 42 | + output.classList.add("correctCaptcha"); |
| 43 | + output.innerHTML = "Correct!"; |
| 44 | + } else { |
| 45 | + output.classList.add("incorrectCaptcha"); |
| 46 | + output.innerHTML = "Incorrect, please try again"; |
| 47 | + } |
| 48 | +}); |
| 49 | + |
| 50 | +// This event listener is stimulated whenever the user press the "Refresh" button |
| 51 | +// A new random CAPTCHA is generated and displayed after the user clicks the "Refresh" button |
| 52 | +refreshButton.addEventListener('click', function() { |
| 53 | + userText.value = ""; |
| 54 | + let refreshArr = []; |
| 55 | + for (let j = 1; j <= 7; j++) { |
| 56 | + refreshArr.push(alphaNums[Math.floor(Math.random() * alphaNums.length)]); |
| 57 | + } |
| 58 | + captchaText.innerHTML = refreshArr.join(''); |
| 59 | + output.innerHTML = ""; |
| 60 | +}); |
0 commit comments