这里我用jsp编写了一个简单的表单。我希望reCAPTCHA和textbox都是“必填”字段。目前,如果我只是填写文本框并按“登录”,它仍然会发送数据。如何使reCAPTCHA成为必需的,即强制的?
<!DOCTYPE html>
<html lang="en">
<head>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<label> Form </label>
<form action = "somefile" method = "post">
<input = "text" name = "textbox1" required>
<div class="g-recaptcha" data-sitekey="6LdliBkTAAAAALmMlPRRm0NtKW3fz2kT2nxiWrVG"></div>
<button type="submit" name="login">Sign In</button>
</form>
</body>
</html>
发布于 2016-11-19 02:35:31
你绝对可以用一些javascript来创建所需的reCaptcha。当然,您仍然需要验证reCaptcha服务器端,但这将添加一些客户端验证。
var captcha_passed =false;
请完成reCaptcha要调用的reCaptcha
document.getElementById("lblCaptchaRequired").hidden on_captcha_filled() { captcha_passed = true;= true;} function
form:
/* captcha_passed = false;var *表单提交处理程序。*显示验证码必需标签并防止提交,直到返回true */ function check_captcha(e) { if ( captcha_passed ) return true;captcha = false;e.preventDefault();} /* * Google reCaptcha数据回调处理程序。*将captcha_passed设置为true以允许提交表单,并隐藏验证码必需的标签。*/ function on_captcha_filled() { captcha_passed = true;document.getElementById("lblCaptchaRequired").hidden = true;}
现在,当您的用户到达该页面时,所需的标签最初是隐藏的,并且您的captcha_passed
被初始化为false
。当用户提交表单时,将调用onsubmit处理程序check_captcha
,它检查captcha_passed
标志并允许提交操作继续,或者停止提交操作并取消隐藏标签。
当用户完成reCaptcha时,将调用on_captcha_filled
函数,该函数将captcha_passed
标志设置为true
并隐藏标签。
发布于 2018-10-14 14:10:23
您好,请在下面找到客户端验证的详细解决方案
HTML表单:我已经添加了文本框google-response,并创建了三个回调事件。1)data-error-callback=“wrongCaptcha”,2)data-callback="correctCaptcha",3)data-expired-callback="wrongCaptcha",
<form class="form-inline" class="text-center">
<div class="form-group">
<label for="email">Email:</label>
<input type="email" id="newsletter-email" class="form-control" id="email">
</div>
<button type="button" class="btn btn-default" onclick="validationNewsletter()">Submit</button>
<input type="hidden" id="google-response"/>
<br/><br/>
<div class="form-group">
<div class="g-recaptcha" id="gcaptcha" data-sitekey="XXXX-XXXX" data-error-callback="wrongCaptcha" data-callback="correctCaptcha" data-expired-callback="wrongCaptcha"></div>
</div>
</form>
JS验证
<script>
function validationNewsletter() {
if($('#google-response').val()==""||$("[name=g-recaptcha-response]").val()==""){
alert("Please check google recaptcha");
return false;
}
if($('#google-response').val()!=$("[name=g-recaptcha-response]").val()){
alert("Sorry spammer");
return false;
}
if(letsPutFormValidation){
alert("Some form validation");
return false;
}
$('#form-id').submit();
}
var correctCaptcha = function (response) {
$('#google-response').val(response);
};
var wrongCaptcha=function(response){
$('#google-response').val('');
}
</script>
https://stackoverflow.com/questions/36123980
复制相似问题