简体   繁体   English

如何在JavaScript中正确验证CAPTCHA?

[英]How to validate CAPTCHA correctly in JavaScript?

I'm in the middle of coding CAPTCHA in JavaScript, and I'm trying to get the validation for a contact form to work properly. 我正在用JavaScript编码CAPTCHA,正在尝试验证联系表单是否正常工作。 I'm almost there, the form won't be submitted until the CAPTCHA text-field is entered, but the problem is I'm still getting an error message when I entered the CAPTCHA code correctly. 我快到了,在输入验证码文本字段之前,不会提交表单,但是问题是,当我正确输入验证码时,仍然收到错误消息。

<script>
function ValidateContactForm()
{
var name = document.ContactForm.name;
var phone = document.ContactForm.phone;
var code = document.ContactForm.code;

    if (name.value == "")
{
    window.alert("Please enter your name.");
    name.focus();
    return false;
}

if (phone.value == "")
{
    window.alert("Please enter a valid phone number..");
    phone.focus();
    return false;
}

if (code.value == "")
{
    window.alert("Please enter the code as displayed on screen."); 
    code.focus();
    return false;
    }
    else if (code.value != "") 
{
        window.alert("Your code does not match. Please try again."); 
    code.focus();
    return false;
 }

else {
    return true;
}

return true;
}
</script>

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

Check this lines, the problem is here: 检查此行,问题在这里:

if (code.value == "")
{
    window.alert("Please enter the code as displayed on screen."); 
    code.focus();
    return false;
}
else if (code.value != "") 
{
    window.alert("Your code does not match. Please try again."); 
    ^^^^^^^^^^^^^
    code.focus();
    ^^^^^^^^^^^^^
    return false;
    ^^^^^^^^^^^^^
 }
else {

    return true;
}

This code will return false every time. 每次该代码将返回false。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM