-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
60 lines (51 loc) · 2.79 KB
/
script.js
File metadata and controls
60 lines (51 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Selecting necessary DOM elements
const captchaTextBox = document.querySelector(".captch_box input"); // Input field where the generated captcha will be displayed
const refreshButton = document.querySelector(".refresh_button"); // Button to refresh the captcha
const captchaInputBox = document.querySelector(".captch_input input"); // Input field for the user to enter the captcha
const message = document.querySelector(".message"); // Element to display the validation message
const submitButton = document.querySelector(".button"); // Submit button to validate the entered captcha
// Variable to store the generated captcha
let captchaText = null;
// Function to generate the captcha
const generateCaptcha = () => {
const randomString = Math.random().toString(36).substring(2, 7); // Generate a random string
const randomStringArray = randomString.split("");
const changeString = randomStringArray.map((char) =>
Math.random() > 0.5 ? char.toUpperCase() : char
); // Randomly change some characters to uppercase
captchaText = changeString.join(" "); // Join the characters with spaces for a spaced-out appearance
captchaTextBox.value = captchaText; // Display the generated captcha in the input field
console.log(captchaText);
};
const refreshBtnClick = () => {
generateCaptcha(); // Refresh the captcha when the refresh button is clicked
captchaInputBox.value = ""; // Clear the user input field
captchaKeyUpValidate();
};
const captchaKeyUpValidate = () => {
// Toggle the "disabled" class on the submit button based on whether the captcha input field is empty or not
submitButton.classList.toggle("disabled", !captchaInputBox.value);
if (!captchaInputBox.value) message.classList.remove("active"); // Hide the validation message if the captcha input field is empty
};
// Function to validate the entered captcha
const submitBtnClick = () => {
captchaText = captchaText
.split("")
.filter((char) => char !== " ")
.join(""); // Remove spaces from the stored captcha for validation
message.classList.add("active"); // Show the validation message
// Check if the entered captcha text is correct or not
if (captchaInputBox.value === captchaText) {
message.innerText = "Entered captcha is correct"; // Display success message
message.style.color = "#222620"; // Dark green color for success message
} else {
message.innerText = "Entered captcha is not correct"; // Display error message
message.style.color = "#FF2525"; // Bright red color for error message
}
};
// Add event listeners for the refresh button, captchaInputBox, submit button
refreshButton.addEventListener("click", refreshBtnClick);
captchaInputBox.addEventListener("keyup", captchaKeyUpValidate);
submitButton.addEventListener("click", submitBtnClick);
// Generate a captcha when the page loads
generateCaptcha();