Link to home
Start Free TrialLog in
Avatar of GDB08
GDB08

asked on

Simple javascript CAPTCHA verification code

I have a simple CAPTCHA verification code that looks like this:
    <script type="text/javascript">
    var a = Math.ceil(Math.random() * 10);
    var b = Math.ceil(Math.random() * 10);      
    var c = a + b
    function DrawBotBoot()
    {
        document.write("What is "+ a + " + " + b +"? ");
        document.write("<input id='BotBootInput' type='text' maxlength='2' size='2'/>");
    }    
    function ValidBotBoot(){
        var d = document.getElementById('BotBootInput').value;
        if (d == c) return true;        
        return false;    
    }
    </script>
</head>
<body>
Are you human?<br />
<script type="text/javascript">DrawBotBoot()</script>
<input id="Button1" type="button" value="Check" onclick="alert(ValidBotBoot());"/>

I need to combine it to my form so It makes the check and then if true sends the result from the form.
The value of the send button is now like this:
<input value="Send" type="submit" name="submit1" />

How can I change it so it uses the CAPTCHA verification code and then sends the form if true?

Thanks.
Avatar of StealthyDev
StealthyDev

Does the code attached helps you?

<script type="text/javascript">
    var a = Math.ceil(Math.random() * 10);
    var b = Math.ceil(Math.random() * 10);      
    var c = a + b
    function DrawBotBoot()
    {
        document.write("What is "+ a + " + " + b +"? ");
        document.write("<input id='BotBootInput' type='text' maxlength='2' size='2'/>");
    }    
    function ValidBotBoot(){
        var d = document.getElementById('BotBootInput').value;
        if (d == c) return true;        
        return false;    
    }
    </script>
</head>
<body>
<form action="captcha-submitpage.html">
	Are you human?<br />
	<script type="text/javascript">DrawBotBoot()</script>
	<input value="Send" type="submit" name="submit1" onclick="return ValidBotBoot();"/>
</form>

Open in new window

The secret is:

If you return false to submit button, the form will not be submitted !

Cheers.
Avatar of GDB08

ASKER

Thanks a lot, this works :-)

Would it be complicated to add an alert if people add the wrong sum?
Like: This is wrong, please try again?
ASKER CERTIFIED SOLUTION
Avatar of StealthyDev
StealthyDev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GDB08

ASKER

Thanks :-)
:) thanks for your points..

If you are new to JavaScripts and want to learn the beginnings, try the below link.
http://www.w3schools.com/js/js_intro.asp

Best Regards.
PS: forgive if its offensive