使用javascript提交表单
我有一个带有javascript验证程序的登录表单,我也在使用Google invisible Recaptcha。 当我不使用Recaptcha时,表单工作完全正确,但当我使用Recaptcha时,它调用javascript函数来提交表单,我不能在该函数中使用验证器。 我对javascript没有太多的了解,所以如果有任何帮助,我将不胜感激 HTML代码:使用javascript提交表单,javascript,html,forms,validation,recaptcha,Javascript,Html,Forms,Validation,Recaptcha,我有一个带有javascript验证程序的登录表单,我也在使用Google invisible Recaptcha。 当我不使用Recaptcha时,表单工作完全正确,但当我使用Recaptcha时,它调用javascript函数来提交表单,我不能在该函数中使用验证器。 我对javascript没有太多的了解,所以如果有任何帮助,我将不胜感激 HTML代码: <form class="form-horizontal form-material validate-form" id="logi
<form class="form-horizontal form-material validate-form" id="loginform" method="post" action="" role="form">
<h3 class="box-title m-b-20">Login</h3>
<div class="Input-Style">
<div class="wrap-input2 validate-input" data-validate="Email is uvalid">
<input class="input2" id="inputEmail" type="text" name="username" autofocus>
<span class="focus-input2" data-placeholder="your email"></span>
</div>
</div>
<div class="Input-Style">
<div class="wrap-input2 validate-input" data-validate="password is required">
<input class="input2" id="inputPassword" type="password" name="password">
<span class="focus-input2" data-placeholder="your password"></span>
</div>
</div>
<div class="form-group text-center m-t-20">
<div class="col-xs-12">
<button id="login" class="btn btn-info btn-lg btn-block text-uppercase waves-effect waves-light g-recaptcha"
data-sitekey="" data-callback="enableSubmit">Login
</button>
</div>
</div>
</form>
您需要使用
grecaptcha.render
callback。您还需要创建单独的函数来验证表单,如下所示:
function formValidator() {
// validation script will be here
}
并将此函数传递给grecaptcha.render
方法
grecaptcha.render('example3', {
'sitekey' : 'your_site_key',
'callback' : formValidator,
'theme' : 'dark'
});
修改JavaScript的第一行:
[...]
/*==================================================================
[ Validate ]*/
var username = $('.validate-input input[name="username"]');
var password = $('.validate-input input[name="password"]');
var verifyCallback = function(){
var check = true;
[...]
因此,验证函数不绑定到submit事件(由Recaptcha捕获),而是绑定到一个变量
此外,在validateCallback函数之后的这些行:
[...]
return check;
});
grecaptcha.render('html_element', {
'sitekey' : 'your_site_key',
'callback': verifyCallback
});
[...]
现在,首先将检查验证码,然后将作为所谓的“回调”方法执行验证
[...]
return check;
});
grecaptcha.render('html_element', {
'sitekey' : 'your_site_key',
'callback': verifyCallback
});
[...]