0

I am trying to create a simple captcha using javascript by creating a math question. This is not working. Any idea on what I am doing wrong?

What does four + 3 equal? <br>
<input type="text" name="secquestion" id="secquestion" value="" placeholder="Type Numberic Answer Here (Ex. 9)">


 <input type="submit" value="  Send  " class="button" name="button" onclick="if (value !== "7") {alert('You must answer the security question correctly!');return false}">
1
  • Your best solution would be to start by removing the inline JavaScript. It will make it easier to work with going forward. Then reference the code mentioned below...they all basically say the same thing. Commented Apr 17, 2014 at 20:29

4 Answers 4

1

the problem is the double quotes used in your conditional statement are messing with the double quotes around the onclick values. use this

<input type="submit" value=" Send " class="button" name="button" onclick="if (value !== '7') {alert('You must answer the security question correctly!');return false}">

Sign up to request clarification or add additional context in comments.

1 Comment

You solved the syntax error, but the if condition would had been always false since OP uses the button value "send" instead of the textbox value.
1

You needed to remove the double quotes and additionally you need to tell your conditional function which elements value you are interested in.

By using javascripts documentGetElementById we can specify which element on the document we are interested in. Then we select the value attribute in order to grab the input from the user and test it in our conditional statement.

    What does four + 3 equal? <br>
    <input type="text" name="secquestion" id="secquestion" value="" placeholder="Type Numberic Answer Here (Ex. 9)">


     <input type="submit" value="  Send  " class="button" name="button" onclick="if(document.getElementById('secquestion').value != 7) {alert('You must answer the security question correctly!');return false}">

Personally, it might be easier to maintain this code by breaking this conditional out into its own function like so:

    <html>
    What does four + 3 equal? <br>
    <input type="text" name="secquestion" id="secquestion" value="" placeholder="Type Numberic Answer Here (Ex. 9)">


    <input type="submit" value="  Send  " class="button" name="button" onclick="checkCaptcha()">

    <script type="text/javascript">
        function checkCaptcha(){
           if(document.getElementById("secquestion").value != 7){

               alert('You must answer the security question correctly!');

               return false;
           }

           return true;
         }
    </script>
    </html>

Comments

0

The issue is only with doublequotes.

The below code works.

What does four + 3 equal? <br>
<input type="text" name="secquestion" id="secquestion" value="" placeholder="Type Numberic Answer Here (Ex. 9)">


 <input type="submit" value="  Send  " class="button" name="button" onclick="if (value !== '7') {alert('You must answer the security question correctly!');return false}">

JSFiddle

Comments

0

Your quotes are off.

onclick="if (value !== "7")....

needs to be onclick="if (value !== '7')....

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.