JavaScript without “popup” window in script? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of JavaScript without “popup” window in script? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I want the send-button (id=”Button1″) to send a mail only when the caption is correct (string1 === string2). How and were, do I get this to work, in the button itself or in the script?

I also not want the popup to show telling me “TRUE”, same with false, I do want the “alert” to open and tell me when its wrong, but not sign after that telling me “FALSE”. I have tried without the “return false;” and “return true;”, but that only gives me “undefined”. EDIT: Just fixed this, I didn’t realised I had “alert(ValidCaptcha());” it should be “ValidCaptcha();”. But still need help with the first question.

function Captcha(){
         var alpha = new Array('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');
         var i;
         for (i=0;i<6;i++){
             var a = alpha[Math.floor(Math.random() * alpha.length)];
             var b = alpha[Math.floor(Math.random() * alpha.length)];
             var c = alpha[Math.floor(Math.random() * alpha.length)];
             var d = alpha[Math.floor(Math.random() * alpha.length)];
             var e = alpha[Math.floor(Math.random() * alpha.length)];
             var f = alpha[Math.floor(Math.random() * alpha.length)];
             var g = alpha[Math.floor(Math.random() * alpha.length)];
                          }
             var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' '+ f + ' ' + g;
             document.getElementById("mainCaptcha").innerHTML = code
             document.getElementById("mainCaptcha").value = code
           }
    function ValidCaptcha(){
         var string1 = removeSpaces(document.getElementById('mainCaptcha').value);
         var string2 = removeSpaces(document.getElementById('txtInput').value);
         
      if (string1 === string2){
                return true;
         }
      else
      {        
         alert('Wrong, try again');
              return false;
              }
    }
    function removeSpaces(string){
         return string.split(' ').join('');
    }
.capt{
        background-color:LightGray;
        width: 300px;
        height:100px;
        
    }

    #mainCaptcha{
        position: relative;
        left : 60px;
        top: 5px;
        
    }

    #refresh{
        position:relative;
        left:230px;
        width:30px;
        height:30px;
        bottom:45px;
    }

    #txtInput, #Button1{
        position: relative;
        left:20px;
        bottom: 40px;
    }
<body onload="Captcha();"> 
       <div class="capt"> 
       <h2 style="-moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;-o-user-select:none;" unselectable="on" onselectstart="return false;" onmousedown="return false;" type="text" id="mainCaptcha"></h2>
       <p><input type="button" id="refresh" onclick="Captcha();"/></p>            <input type="text" id="txtInput"/>    
       
       <button value="Send" id="Button1" type="submit" onclick="alert(ValidCaptcha());">SEND</button>
       </div>
    </body>

Answer

you are passing ValidCaptcha inside alert that’s why its returning alert with your return True and false I removed the alert call and it works fine

function Captcha(){
         var alpha = new Array('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');
         var i;
         for (i=0;i<6;i++){
             var a = alpha[Math.floor(Math.random() * alpha.length)];
             var b = alpha[Math.floor(Math.random() * alpha.length)];
             var c = alpha[Math.floor(Math.random() * alpha.length)];
             var d = alpha[Math.floor(Math.random() * alpha.length)];
             var e = alpha[Math.floor(Math.random() * alpha.length)];
             var f = alpha[Math.floor(Math.random() * alpha.length)];
             var g = alpha[Math.floor(Math.random() * alpha.length)];
                          }
             var code = a + ' ' + b + ' ' + ' ' + c + ' ' + d + ' ' + e + ' '+ f + ' ' + g;
             document.getElementById("mainCaptcha").innerHTML = code
             document.getElementById("mainCaptcha").value = code
           }
    function ValidCaptcha(){
         var string1 = removeSpaces(document.getElementById('mainCaptcha').value);
         var string2 = removeSpaces(document.getElementById('txtInput').value);
         
      if (string1 === string2){
                return true;
         }
      else
      {        
         alert('Wrong, try again');
              return false;
              }
    }
    function removeSpaces(string){
         return string.split(' ').join('');
    }
.capt{
        background-color:LightGray;
        width: 300px;
        height:100px;
        
    }

    #mainCaptcha{
        position: relative;
        left : 60px;
        top: 5px;
        
    }

    #refresh{
        position:relative;
        left:230px;
        width:30px;
        height:30px;
        bottom:45px;
    }

    #txtInput, #Button1{
        position: relative;
        left:20px;
        bottom: 40px;
    }
<body onload="Captcha();"> 
       <div class="capt"> 
       <h2 style="-moz-user-select: none; -webkit-user-select: none; -ms-user-select:none; user-select:none;-o-user-select:none;" unselectable="on" onselectstart="return false;" onmousedown="return false;" type="text" id="mainCaptcha"></h2>
       <p><input type="button" id="refresh" onclick="Captcha();"/></p>            <input type="text" id="txtInput"/>    
       
       <button value="Send" id="Button1" type="submit" onclick="ValidCaptcha();">SEND</button>
       </div>
    </body>
We are here to answer your question about JavaScript without “popup” window in script? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji