JS验证验证码?

oyzz1988 2009-07-23 08:22:29
怎么在表单前验证用户输入的验证码是否输入正确?不提交到后台验证
验证码生成后,放在session对象 中,在线等 ,附代码最好
...全文
1015 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
IT爱好者 2010-12-22
  • 打赏
  • 举报
回复
有没有纯JS的带图片的验证代码啊?
lishadafei 2010-05-29
  • 打赏
  • 举报
回复
我也碰到了这个问题,有大神建议先用随机数生成验证码,再将验证码写到图片中去,这样session中存的便是数字,就不会出现刷新后获得的是前一个验证码了!
lishadafei 2010-05-29
  • 打赏
  • 举报
回复
lz头像好猥琐!
oyzz1988 2009-07-23
  • 打赏
  • 举报
回复
谢了 各位。 我是想在提交表单前验证 都给分
zyongking 2009-07-23
  • 打赏
  • 举报
回复
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
Random ran=new Random();
ServletOutputStream out=response.getOutputStream();
BufferedImage img=new BufferedImage(60,30,BufferedImage.TYPE_INT_RGB);
Graphics g=img.getGraphics();
//g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
g.fillRect(0,0,60,30);
g.setFont(new Font("黑体",Font.PLAIN,25));
g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
StringBuffer str=new StringBuffer();
for (int i = 0; i < 4 ; i++) {
str.append((char)(ran.nextInt(26)+65));
}
g.drawString(str.toString(),5,25);
for (int i = 0; i <= 10 ; i++) {
//g.setColor(new Color(ran.nextInt(255),ran.nextInt(255),ran.nextInt(255)));
g.drawLine(ran.nextInt(img.getWidth()),ran.nextInt(img.getHeight()),ran.nextInt(img.getWidth()),ran.nextInt(img.getHeight()));
}
JPEGImageEncoder jpeg=JPEGCodec.createJPEGEncoder(out);
jpeg.encode(img);
out.close();
request.getSession().setAttribute("code",str.toString());
}
上面是出验证码,是用servlet写的,,一样的
<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
<script type="">
function change()
{
document.getElementById("imga").src="checkservlet?"+Math.random();
}
</script>
</head>
<body bgcolor="#ffffff">
<h1>
验证码
</h1>
<form method="post" action="jsp2.jsp">
<br>
<img id="imga" alt="" src="checkservlet" />
<a href="javascript:change();">看不清楚,换一张</a>
<br>
验证码:<input type="text" name="cd"/>
<input type="Submit" name="Submit" value="Submit">
</form>
</body>
</html>
上面的是调用
<%@page contentType="text/html; charset=GBK"%>
<%
String code = request.getParameter("cd");
String check=session.getAttribute("code").toString();
if (!code.equals(check)) {
out.println("<script>alert('验证码错误!');history.back();</script>");
}
else {
out.println("<script>alert('验证码正确!');history.back();</script>");
}
%>
这个是处理,,你可以看一下
oyzz1988 2009-07-23
  • 打赏
  • 举报
回复

没其它办法吗?

我们还没学到ajax。。。= =!
浪尖赏花 2009-07-23
  • 打赏
  • 举报
回复
取到老一张的图片,是因为<img id="img" src="code.jsp" >要在加载图片的时候才会去调用code.jsp
而这个时候首页的jsp在服务器端已经执行,并生成页面到客户端
所以取的是之前一张的图片
LZ可以用ajax再调用一个JSP,在那个JSP里取SESSION
oyzz1988 2009-07-23
  • 打赏
  • 举报
回复
<img id="img" src="code.jsp" >我是在前台用图片的SRC链接验证码的JSP

但在前台用SESSION取验证码的时候 ,取到的验证码老是前一张,不能同步
浪尖赏花 2009-07-23
  • 打赏
  • 举报
回复
要在JS中验证的话,后台生成验证码后,必须要放到前台页面(作为隐藏域、或者JS变量)

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧