首先提出一个问题:在做网站开发的时候,用到了验证码来防止恶意提交表单,那么要如何实现当验证码错误时,只是刷新一下验证码,而其它填写的信息不改变?

  先说一下为什么有这个需求:以提交注册信息页面为例,一般注册都需要用户填一个验证码信息(防止机器恶意注册),并且这个验证码会提交到后台去进行比对,若是错了则不会检查其他提交信息而直接返回浏览器端提示验证码错误。若是简单地用form表单直接将数据提交到指定的url,当验证码填写错误的信息返回浏览器端的时候,不可避免整个页面都会重新刷新一次,这是用户所不想要的,因为其它的一些正确信息还需要重新再填一次,这样就造成用户体验不太好。而这个问题就可以通过Ajax异步提交表单来实现。(这只是其中一种解决方案)

  下面就来看看具体的实现:

  前台Html代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax异步提交表单之检查验证码</title>
</head>
<body> <form action="javascript:AjaxPostData()" method="post">
<label>用户名:</label>
<input name="account" class="form-control" id="account" type="text" placeholder="用户名" required="required" />
<label>密码:</label>
<input name="password" class="form-control" id="password" type="password" placeholder="密码" required="required" />
<label>验证码:</label>
<img id="valiCode" class="validcode" src="/Home/GetValidateCode" alt="验证码" title="点击刷新" />
<input name="code" class="form-control" id="code" type="text" placeholder="验证码" required="required" />
<button type="submit">提交</button>
</form> <script src="~/Scripts/jquery-1.12.1.min.js"></script>
<script>
//刷新验证码
function RefreshValiCode() {
document.getElementById("valiCode").src = "/GetValidateCode?time=" + (new Date()).getTime();
} function AjaxPostData()
{
var code = document.getElementById("code").value;
var account = document.getElementById("account").value;
var password = document.getElementById("password").value;
$.ajax({
url: '/User/Register',//数据提交到的目标url
type: 'post',//post方式提交
async: true,//异步提交
data: {account: account, password: password, code: code },//提交的数据
success: function (data) {//发送成功的回调函数
if (data.success) {
alert("注册失败!");
}
else {
alert("注册成功!");
RefreshValiCode();//刷新验证码
document.getElementById("code").value = "";//置空输入框
}
},
error: function () {
alert("请求失败!请重新提交!");
}
});
}
</script>
</body>
</html>

  注:jquery-1.12.1.min.js需要自己下载引用。

  后台C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security; namespace Test.Controllers
{
public class UserController : Controller
{
/// <summary>
/// 注册
/// </summary>
/// <param name="code">验证码</param>
/// <returns></returns>
[HttpPost]
public ActionResult Register(string name, string password, string code)
{
//Session["RegisterCode"]在生成验证码的时候设置其值
if (string.IsNullOrWhiteSpace(code) || Session["RegisterCode"].ToString().ToUpper() != code.ToUpper())
{
return Json(new { success = false});
}
else
{
//其它操作...
return Json(new { success = true});
} }
}
}

  此次知识分享就到这,敬请期待下一次的分享。^_^

<我的博客主页>:http://www.cnblogs.com/forcheng/

<Wing工作室主页>:http://www.wingstudio.org/

最新文章

  1. LaTeX_fleqn参数时,多行公式对齐居中的同时选择性的加编号
  2. OpenLayers 3 中Layers的相关知识
  3. URL详解与URL编码
  4. 【matlab】输出固定位数的数字
  5. 【温故而知新-Javascript】使用 Document 对象
  6. 文件已经加入.gitignore但是vs并没有显示文件处于ignore状态
  7. Android新浪微博客户端(三)——添加多个账户及认证
  8. HDU 1290 献给杭电五十周年校庆的礼物
  9. 我们究竟什么时候可以使用Ehcache缓存(转)
  10. More DETAILS! PBR的下一个发展在哪里?
  11. servlet filter中使用autowired无法注入
  12. Web服务中延时对QoE(体验质量)的影响
  13. css公共库——简介中超过长度显示省略号
  14. JS学习笔记Day26
  15. 论文翻译:Ternary Weight Networks
  16. webpack4.0各个击破(4)—— Javascript &amp; splitChunk
  17. Confluence 6 查看系统信息
  18. ECMAScript各版本简介及特性
  19. 缓存穿透、雪崩、热点与Redis
  20. BT.656 NTSC制式彩条生成模块(verilog)

热门文章

  1. java内功 ---- jvm虚拟机原理总结,侧重于GC
  2. RSS与公众号
  3. 我心中的核心组件(可插拔的AOP)~消息组件~完善篇
  4. Permission is only granted to system apps
  5. CAS原子锁 高效自旋无锁的正确用法
  6. 快速入门系列--WCF--07传输安全、授权与审核
  7. 解析大型.NET ERP系统 20条数据库设计规范
  8. 解析Visual Studio 2015促进生产力的10个新功能
  9. Strophe.js连接XMPP服务器Openfire、Tigase实现Web私聊、群聊(MUC)
  10. Moon River