如何在MVC 3中使Recaptcha成为必填字段?

khe*_*eya 8 asp.net validation jquery asp.net-mvc-3

我已经在我的注册表格上重新登记了.用户必须在重新记录框中键入内容才能将其发布到服务器.我有其他领域的服务器端验证和客户端验证.

从rcaptcha生成的html:

<input type="text" id="recaptcha_response_field" name="recaptcha_response_field" autocomplete="off" class="valid">
Run Code Online (Sandbox Code Playgroud)

视图:

@ReCaptcha.GetHtml(theme: "red", publicKey: GetPublicKey())
@Html.ValidationMessageFor(m => m.recaptcha_response_field)
Run Code Online (Sandbox Code Playgroud)

模型:

[Required(ErrorMessage = "'captcha required' ")]
public string recaptcha_response_field { get; set; }
Run Code Online (Sandbox Code Playgroud)

在加载时,我看到'需要验证码'消息.但如果我输入某些东西,它仍会出现.表单将使用空的recaptcha框提交.

如何在客户端进行recaptcha必填字段?

谢谢你的帮助

编辑: 我添加此拦截提交btn点击事件,但它不会停止发布表单.

    <button type="submit" id="btnSubmit" class="sprt bg_red bt_red h25" onsubmit="ValidateAndSubmit();">Signup</button>
<script type="text/javascript">    
    function ValidateAndSubmit() {
            var v=$('#recaptcha_response_field').val();
            if (v == '' || v == undefined) {
                alert('captcha is required');
                return false;
            }
            return true;
        }
</script>
Run Code Online (Sandbox Code Playgroud)

Ari*_*tos 2

要使其在您键入时工作,请返回该值,而不仅仅是调用它。类型

onsubmit="return ValidateAndSubmit();"
Run Code Online (Sandbox Code Playgroud)

或者

onclick="return ValidateAndSubmit();"
Run Code Online (Sandbox Code Playgroud)

您也可以尝试在表单标签上添加相同的验证。