Ajax校验验证码(可以设置为数字、字母、中文汉字等,背景图有干扰线)
来源:互联网 发布:效果图制作软件排行 编辑:程序博客网 时间:2024/06/06 08:27
一般处理程序,获得验证码
/// <summary> /// VCode 的摘要说明 /// </summary> public class VCode : IHttpHandler,System.Web.SessionState.IRequiresSessionState { public void ProcessRequest(HttpContext context) { //System.Threading.Thread.Sleep(5000); context.Response.ContentType = "image/jpeg"; string code = GetRndNumStr(); //string code = GetRndNum(); //string code = GetRndStr(); //string code = GetRndCh(); context.Session["VCode"] = code; using (Bitmap img = CreateImages(code, "ch")) { img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); } } // 数字+字母随机 private string GetRndNumStr() { string Vchar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,0,1,2,3,4,5,6,7,8,9"; string[] VcArray = Vchar.Split(','); string checkCode = string.Empty; Random rand = new Random(); for (int i = 0; i < 4; i++) { int t = rand.Next(VcArray.Length); checkCode += VcArray[t]; } return checkCode; } // 数字随机数 private string GetRndNum() { string code = string.Empty; Random random = new Random(); for (int i = 0; i < 4; i++) { code += random.Next(9); } return code; } // 英文随机 private string GetRndStr() { string Vchar = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"; string[] VcArray = Vchar.Split(','); string checkCode = string.Empty; Random rand = new Random(); for (int i = 0; i < 4; i++) { int t = rand.Next(VcArray.Length); checkCode += VcArray[t]; } return checkCode; } // 中文随机 private string GetRndCh() { System.Text.Encoding gb = System.Text.Encoding.Default;//获取GB2312编码页(表) object[] bytes = CreateRegionCode(4);//生4个随机中文汉字编码 string[] str = new string[4]; System.Text.StringBuilder sb = new System.Text.StringBuilder(); for (int i = 0; i < 4; i++) { //根据汉字编码的字节数组解码出中文汉字 str[i] = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[]))); sb.Append(str[i].ToString()); } return sb.ToString(); } // 产生随机中文字符 private static object[] CreateRegionCode(int strlength) { //定义一个字符串数组储存汉字编码的组成元素 string[] rBase = new String[16] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; Random rnd = new Random(); object[] bytes = new object[strlength]; for (int i = 0; i < strlength; i++) { //区位码第1位 int r1 = rnd.Next(11, 14); string str_r1 = rBase[r1].Trim(); //区位码第2位 rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i); int r2; if (r1 == 13) { r2 = rnd.Next(0, 7); } else { r2 = rnd.Next(0, 16); } string str_r2 = rBase[r2].Trim(); //区位码第3位 rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机种子 int r3 = rnd.Next(10, 16); string str_r3 = rBase[r3].Trim(); //区位码第4位 rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i); int r4; if (r3 == 10) { r4 = rnd.Next(1, 16); } else if (r3 == 15) { r4 = rnd.Next(0, 15); } else { r4 = rnd.Next(0, 16); } string str_r4 = rBase[r4].Trim(); //定义两个字节变量存储产生的随机汉字区位码 byte byte1 = Convert.ToByte(str_r1 + str_r2, 16); byte byte2 = Convert.ToByte(str_r3 + str_r4, 16); //将两个字节变量存储在字节数组中 byte[] str_r = new byte[] { byte1, byte2 }; //将产生的一个汉字的字节数组放入object数组中 bytes.SetValue(str_r, i); } return bytes; } // 画图片的背景图+干扰线 private Bitmap CreateImages(string checkCode, string type) { int step = 0; if (type == "ch") { step = 5;//中文字符,边界值做大 } int iwidth = (int)(checkCode.Length * (13 + step)); System.Drawing.Bitmap image = new System.Drawing.Bitmap(iwidth, 22); Graphics g = Graphics.FromImage(image); g.Clear(Color.White);//清除背景色 Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };//定义随机颜色 string[] font = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" }; Random rand = new Random(); for (int i = 0; i < 50; i++) { int x1 = rand.Next(image.Width); int x2 = rand.Next(image.Width); int y1 = rand.Next(image.Height); int y2 = rand.Next(image.Height); g.DrawLine(new Pen(Color.LightGray, 1), x1, y1, x2, y2);//根据坐标画线 } for (int i = 0; i < checkCode.Length; i++) { int cindex = rand.Next(7); int findex = rand.Next(5); Font f = new System.Drawing.Font(font[findex], 10, System.Drawing.FontStyle.Bold); Brush b = new System.Drawing.SolidBrush(c[cindex]); int ii = 4; if ((i + 1) % 2 == 0) { ii = 2; } g.DrawString(checkCode.Substring(i, 1), f, b, 3 + (i * (12 + step)), ii); } g.DrawRectangle(new Pen(Color.Black, 0), 0, 0, image.Width - 1, image.Height - 1); System.IO.MemoryStream ms = new System.IO.MemoryStream(); return image; } public bool IsReusable { get { return false; } } }HTML代码
验证码: <input id="txtVcode" type="text" /> <img alt="" id="imgCode" src="/js/vcode/VCode.ashx" /> <a id="aCode" href="javascript:void(0)">看不清,换一张</a> <div id="txtVcodeTip"></div>Javascript变换验证码代码
function getVCode() { $("#imgCode").attr("src", "/js/vcode/VCode.ashx?r=" + new Date());}$(function () { //验证码 var imgCode = $("#imgCode"); imgCode.attr("alt", "点击获取验证码"); imgCode.attr("title", "点击更换验证码"); imgCode.css("cursor", "pointer"); imgCode.click(function () {//点击验证码本身切换验证码 getVCode(); }); //点击文字超链接更换验证码 var aCode = $("#aCode"); aCode.click(function () { getVCode(); }); //验证码提示消息 var vcodeMsg = $("#txtVcodeTip"); vcodeMsg.css("color", "Red"); var vcode = $("#txtVcode"); vcode.blur(function () { if ($.trim(vcode.val()) == '') { vcodeMsg.text("验证码不能为空"); return false; } else { $.post("/js/vcode/CheckVCode.ashx", { "vcode": $.trim(vcode.val()) }, function (data) { vcodeMsg.text(data); }); } }).focus(function () { vcodeMsg.text(""); });});
CheckVCode.ashx代码
public class CheckVCode : IHttpHandler, System.Web.SessionState.IRequiresSessionState { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string vcode = context.Request["vcode"]; context.Response.Write(CheckCode(vcode)); } public static string CheckCode(string vcode) { if (HttpContext.Current.Session["VCode"] != null) { string sysCode = HttpContext.Current.Session["VCode"].ToString(); //使用 Compare 方法,这个方法可以做大小写忽略的比较,并且不会创建新字符串 if (String.Compare(vcode, sysCode, true) == 0) { return "ok"; } else { return "验证码输入错误,重新输入"; } } else { return "验证码过期,请更换验证码"; } } public bool IsReusable { get { return false; } } }
- Ajax校验验证码(可以设置为数字、字母、中文汉字等,背景图有干扰线)
- 很棒的验证码(可以任意设置相关属性)(汉字,英文,数字等)
- 字母数字、字母、汉字验证码 (java)
- 字母数字、字母、汉字验证码 (java)
- 字母数字、字母、汉字验证码 (java)
- 数字,字母,汉字的校验
- js 验证只能为中文、数字、字母
- js 验证只能为中文、数字、字母
- 比较全的验证码类(数字,字母,汉字)
- 比较全的验证码类(数字,字母,汉字)
- PHP数字字母汉字验证码实现
- 验证码类 数字 字母 汉字
- easyui 自定义表单内容验证(汉字、字母、数字、邮箱、电话、邮编、身份证号等)
- easyui 自定义表单内容验证(汉字、字母、数字、邮箱、电话、邮编、身份证号等)
- 给大家分享一下自己写的验证码api,有汉字、字母数字两类
- js验证数字汉字字母
- js验证汉字,字母,数字
- Android自动生成验证码View,支持数字、字母、中文等不同验证码类型
- 构造函数定义为private 或者protected
- (1)前言——(16)盗版(Piracy)
- 【CSDN浅墨】读书笔记4
- Windows 8.1 RTM Core + Pro zh-CN x64 百度盘下载
- 整合IIS与tomcat(共用80端口的配置方法)
- Ajax校验验证码(可以设置为数字、字母、中文汉字等,背景图有干扰线)
- (1)前言——(17)问题(Questions)
- 嵌入式linux下CC1101的驱动 arm9 tq2440开发板
- 自定义RatingBar
- Windows下Apache与Tomcat的整合
- linux基本命令
- OCP-1Z0-052-V8.02-92题
- php 遍历文件夹及文件,获取文件名和文件路径存入数据库中
- UML