用js判断验证码输入的是否正确
页面中,用的是客户端控件,publicpartialclass生成验证码:System.Web.UI.Page{privatestringcode="";protecte...
页面中,用的是客户端控件,public partial class 生成验证码 : System.Web.UI.Page
{
private string code = "";
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
ini();
}
}
private void ini()
{
code = getText();
creatImage(code);
Response.Cookies.Add (new HttpCookie ("mycode",code));
}
/// <summary>
/// 获取随机数
/// </summary>
/// <returns></returns>
private string getText()
{
string re = "";
List <string>li=new List<string> ();
for (int i = 65; i <= 90; i++)
{
char c = (char)i;
li.Add(c.ToString());
}
for (int i = 0; i <= 9; i++)
{
li.Add(i.ToString());
}
int count = li.Count ;
Random rd = new Random();
int cindex = 0;
for (int i = 0; i < 4; i++)
{
cindex = rd.Next(count - i);//保证没有重复的数
re += li[cindex];
li.Remove(li[cindex]);
}
return re;
}
private void creatImage(string code)
{
if (code.Length == 0)
{
return;
}
Bitmap bmp = new Bitmap((int)Math.Ceiling (code.Length *12.5 ),22);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
Random rd1=new Random ();
for (int i = 0; i < 30; i++)
{
int x1 = rd1.Next(bmp.Width );
int y1 = rd1.Next(bmp.Height);
int x2 = rd1.Next(bmp.Width);
int y2 = rd1.Next(bmp.Height);
g.DrawLine(Pens.Silver, x1, y1, x2, y2);
}
Font f = new Font("宋体", 14, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush br = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle (0,0,bmp.Width,bmp.Height ),Color.Blue ,Color.DarkRed ,1.2f,true);
g.DrawString(code, f, br, 2, 2);
g.DrawRectangle(Pens.Black, 0, 0, bmp.Width - 1, bmp.Height - 1);
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
bmp.Dispose();
}
}这是验证码的生成代码,当在页面中单击按钮的时候,需要判断text里所填的验证码是否正确,怎么用js判断啊。谢谢大家,希望大家说的详细些。我会再给分的。最好是已经测试好了的代码,谢谢 展开
{
private string code = "";
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
ini();
}
}
private void ini()
{
code = getText();
creatImage(code);
Response.Cookies.Add (new HttpCookie ("mycode",code));
}
/// <summary>
/// 获取随机数
/// </summary>
/// <returns></returns>
private string getText()
{
string re = "";
List <string>li=new List<string> ();
for (int i = 65; i <= 90; i++)
{
char c = (char)i;
li.Add(c.ToString());
}
for (int i = 0; i <= 9; i++)
{
li.Add(i.ToString());
}
int count = li.Count ;
Random rd = new Random();
int cindex = 0;
for (int i = 0; i < 4; i++)
{
cindex = rd.Next(count - i);//保证没有重复的数
re += li[cindex];
li.Remove(li[cindex]);
}
return re;
}
private void creatImage(string code)
{
if (code.Length == 0)
{
return;
}
Bitmap bmp = new Bitmap((int)Math.Ceiling (code.Length *12.5 ),22);
Graphics g = Graphics.FromImage(bmp);
g.Clear(Color.White);
Random rd1=new Random ();
for (int i = 0; i < 30; i++)
{
int x1 = rd1.Next(bmp.Width );
int y1 = rd1.Next(bmp.Height);
int x2 = rd1.Next(bmp.Width);
int y2 = rd1.Next(bmp.Height);
g.DrawLine(Pens.Silver, x1, y1, x2, y2);
}
Font f = new Font("宋体", 14, FontStyle.Bold | FontStyle.Italic);
System.Drawing.Drawing2D.LinearGradientBrush br = new System.Drawing.Drawing2D.LinearGradientBrush(new Rectangle (0,0,bmp.Width,bmp.Height ),Color.Blue ,Color.DarkRed ,1.2f,true);
g.DrawString(code, f, br, 2, 2);
g.DrawRectangle(Pens.Black, 0, 0, bmp.Width - 1, bmp.Height - 1);
bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
bmp.Dispose();
}
}这是验证码的生成代码,当在页面中单击按钮的时候,需要判断text里所填的验证码是否正确,怎么用js判断啊。谢谢大家,希望大家说的详细些。我会再给分的。最好是已经测试好了的代码,谢谢 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询