【发布时间】:2016-01-02 17:23:40
【问题描述】:
我正在 MVC 6 中开发一个联系表单,我正在使用 Google reCaptcha v2 进行身份验证,并且表单正在通过 Ajax 提交。我想要的是如何验证 reCaptcha 控件?它工作正常,但我没有找到任何类似于模型验证或 jQuery 验证的验证。任何人都可以告诉我如何在客户端或服务器端验证它。
这是我的代码
家庭控制器:
[HttpPost]
public IActionResult Contact(ContactViewModel model)
{
string EncodedResponse = Request.Form["captchaResponse"];
bool IsCaptchaValid = (CaptchaResponse.Validate(EncodedResponse)); // it is working and returns true if I check the reCaptcha with Google Server.
if (IsCaptchaValid)
{
}
return View();
}
查看:
<form name="sentMessage" asp-controller="Home" asp-action="Contact" id="contactForm" novalidate method="post">
<div class="row control-group">
<div class="font-light form-group col-xs-12 floating-label-form-group controls">
<div class="g-recaptcha" data-sitekey="6LdKRxQTAAAAAHd9kNw8qp4OyxfWxvLnAhxVr8mu"></div>
<br />
<span class="text-danger" id="ValidMessage"></span> <!--Here I want to display validation messge-->
</div>
</div>
<br />
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-default">Send</button>
</div>
</div>
这是我的 JavaScript 文件:
$.ajax({
url: "/Home/Contact",
type: "POST",
data: {
name: name,
subject: subject,
email: email,
message: message,
captcharesponse: captchaResponse
},
cache: false,
success: function() {
// Success message
$('#success').html("<div class='alert alert-success'>");
$('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-success')
.append("<strong>Your message has been sent. </strong>");
$('#success > .alert-success')
.append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('#success').html("<div class='alert alert-danger'>");
$('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×")
.append("</button>");
$('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
$('#success > .alert-danger').append('</div>');
//clear all fields
$('#contactForm').trigger("reset");
},
})
【问题讨论】:
-
谢谢。其实这个帖子我已经看过好几遍了。但是过不去。我只想根据上面给定的场景显示一条验证消息。
标签: jquery asp.net asp.net-mvc validation asp.net-core-mvc