@@ -11,6 +11,8 @@ let submitButton = document.getElementById('submitButton');
1111let output = document . getElementById ( 'output' ) ;
1212let refreshButton = document . getElementById ( 'refreshButton' ) ;
1313
14+ var captchaStr = "" ;
15+
1416
1517// alphaNums contains the characters with which you want to create the CAPTCHA
1618let alphaNums = [ 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' ,
@@ -23,54 +25,49 @@ let alphaNums = ['A', 'B', 'C', 'D', 'E', 'F', 'G',
2325 'x' , 'y' , 'z' , '0' , '1' , '2' , '3' ,
2426 '4' , '5' , '6' , '7' , '8' , '9' ] ;
2527
28+ function generate_captcha ( ) {
29+ // This loop generates a random string of 7 characters using alphaNums
30+ // Further this string is displayed as a CAPTCHA
31+ let emptyArr = [ ] ;
32+
33+ for ( let i = 1 ; i <= 7 ; i ++ ) {
34+ emptyArr . push ( alphaNums [ Math . floor ( Math . random ( ) * alphaNums . length ) ] ) ;
35+ }
36+
37+ captchaStr = emptyArr . join ( '' ) ;
38+
39+ ctx . clearRect ( 0 , 0 , captchaText . width , captchaText . height ) ;
40+ ctx . fillText ( captchaStr , captchaText . width / 4 , captchaText . height / 2 ) ;
2641
27- // This loop generates a random string of 7 characters using alphaNums
28- // Further this string is displayed as a CAPTCHA
29- let emptyArr = [ ] ;
30- for ( let i = 1 ; i <= 7 ; i ++ ) {
31- emptyArr . push ( alphaNums [ Math . floor ( Math . random ( ) * alphaNums . length ) ] ) ;
42+ output . innerHTML = "" ;
43+ }
44+
45+ generate_captcha ( ) ;
46+
47+ function check_captcha ( ) {
48+ if ( userText . value === captchaStr ) {
49+ output . className = 'correctCaptcha' ;
50+ output . innerHTML = "Correct!" ;
51+ } else {
52+ output . className = 'incorrectCaptcha' ;
53+ output . innerHTML = "Incorrect, please try again!" ;
54+ }
3255}
33- var c = emptyArr . join ( '' ) ;
34- ctx . fillText ( emptyArr . join ( '' ) , captchaText . width / 4 , captchaText . height / 2 ) ;
3556
3657// This event listener is stimulated whenever the user press the "Enter" button
3758// "Correct!" or "Incorrect, please try again!" message is
3859// displayed after validating the input text with CAPTCHA
3960userText . addEventListener ( 'keyup' , function ( e ) {
4061 if ( e . key === 'Enter' ) {
41- if ( userText . value === c ) {
42- output . classList . add ( "correctCaptcha" ) ;
43- output . innerHTML = "Correct!" ;
44- } else {
45- output . classList . add ( "incorrectCaptcha" ) ;
46- output . innerHTML = "Incorrect, please try again!" ;
47- }
62+ check_captcha ( ) ;
4863 }
4964} ) ;
5065
5166// This event listener is stimulated whenever the user clicks the "Submit" button
5267// "Correct!" or "Incorrect, please try again!" message is
5368// displayed after validating the input text with CAPTCHA
54- submitButton . addEventListener ( 'click' , function ( ) {
55- if ( userText . value === c ) {
56- output . classList . add ( "correctCaptcha" ) ;
57- output . innerHTML = "Correct!" ;
58- } else {
59- output . classList . add ( "incorrectCaptcha" ) ;
60- output . innerHTML = "Incorrect, please try again!" ;
61- }
62- } ) ;
69+ submitButton . addEventListener ( 'click' , check_captcha ) ;
6370
6471// This event listener is stimulated whenever the user press the "Refresh" button
6572// A new random CAPTCHA is generated and displayed after the user clicks the "Refresh" button
66- refreshButton . addEventListener ( 'click' , function ( ) {
67- userText . value = "" ;
68- let refreshArr = [ ] ;
69- for ( let j = 1 ; j <= 7 ; j ++ ) {
70- refreshArr . push ( alphaNums [ Math . floor ( Math . random ( ) * alphaNums . length ) ] ) ;
71- }
72- ctx . clearRect ( 0 , 0 , captchaText . width , captchaText . height ) ;
73- c = refreshArr . join ( '' ) ;
74- ctx . fillText ( refreshArr . join ( '' ) , captchaText . width / 4 , captchaText . height / 2 ) ;
75- output . innerHTML = "" ;
76- } ) ;
73+ refreshButton . addEventListener ( 'click' , generate_captcha ) ;
0 commit comments