如何实现支付宝验证码实时验证的特效 先上个图:就是当出现四个验证码的时候去后台验证,验证结果以图片的形式显示在输入框的后面,这是怎么弄的?支付宝验证码图片 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 ajax发送验证码到动态页验证 输入框失去焦点并输入框内容不为空是调用ajax,如果输入的难码不对,就把一个"x"的背景图片显示在输入框里 偷懒直接设置input对象的背景图片就行了淘宝的是通过父容器position:relative定位,xx图片position:absolute来弄,有点罗嗦 设背景图片,这个主意可以试一下,那下面淘宝的做法能说详细点吗?css我也是菜鸟 <div style="position:relative"><input type="text" style="width:120px"/><img src="x的图片地址" style="position:absolute;left:110px;top:0px" alt="x"/></div>楼主还是去看下css,要不给你代码你也搞不懂 package com.tarena.netctoss.action;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.util.Random;import com.sun.image.codec.jpeg.ImageFormatException;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class VerifiCodeAction extends BaseAction { private InputStream imgCodeStream; private String verifiCode; private boolean ok = false; private static int WIDTH = 80; private static int HEIGHT = 30; private static int NUM = 5; private static char[] seq = { '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' }; public InputStream getImgCodeStream() { return imgCodeStream; } public void setImgCodeStream(InputStream imgCodeStream) { this.imgCodeStream = imgCodeStream; } public boolean isOk() { return ok; } public void setOk(boolean ok) { this.ok = ok; } public String getVerifiCode() { return verifiCode; } public void setVerifiCode(String verifiCode) { this.verifiCode = verifiCode; } public String code() throws Exception { String code = imageCode(); // 把code放入Session中 session.put(KEY_VERIFICODE, code); System.out.println(code); return "success"; } public String verify() throws Exception { if (verifiCode != null && verifiCode.length() > 0) { String code = (String) session.get(KEY_VERIFICODE); if (verifiCode.equals(code)) { ok = true; } } return "success"; } private String imageCode() throws ImageFormatException, IOException { Random r = new Random(); // 图片的内存映像 BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); // 获得画笔对象 Graphics g = image.getGraphics(); g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); g.fillRect(0, 0, WIDTH, HEIGHT); g.setColor(new Color(0, 0, 0)); // 用于存储随机生成的验证码 StringBuffer number = new StringBuffer(); // 绘制验证码 for (int i = 0; i < NUM; i++) { g.setColor(new Color(r.nextInt(10), r.nextInt(10), r.nextInt(10))); int h = (int) ((HEIGHT * 20 / 100) * r.nextDouble() + (HEIGHT * 80 / 100)); g.setFont(new Font(null, Font.BOLD | Font.ITALIC, h)); String ch = String.valueOf(seq[r.nextInt(seq.length)]); number.append(ch); g.drawString(ch, i * WIDTH / NUM * 90 / 100, h); } //绘制干扰线// for (int i = 0; i <= 12; i++) {// g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));// g.drawLine(r.nextInt(WIDTH), r.nextInt(HEIGHT), r.nextInt(WIDTH), r// .nextInt(HEIGHT));// } ByteArrayOutputStream baos = new ByteArrayOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos); encoder.encode(image); imgCodeStream = new ByteArrayInputStream(baos.toByteArray()); return number.toString(); }}<img src="verificode.action" onclick="this.src='verificode.action?'+(new Date()).getTime()" />Js://判断必须填写内容$.fn.required = function (msg, msgCnt) { var value = this.val(); if (value != null && value.length > 0) { $(msgCnt).text(""); return true; } else { $(msgCnt).text(msg); return false; }};jsp: $(function() { $("#submit").click(function() { var b1 = $("#user").required("用户名必须填写",$("#msg_user")); var b2 = $("#pwd").minLength(3,"密码长度必须大于等于3位",$("#msg_pwd")); var b3 = $("#verificode").required("验证码必须填写",$("#msg_verificode")); var b4 = false; if(b3) { b4 = $("#verificode").remote("verify.action","验证码错误",$("#msg_verificode")); } if(b1 && b2 && b4) { $("#loginForm").submit(); } }); }); 可是只是这样设置的话,x的图片变成绝对定位了,这样不好,万一窗口变小布局整个都不行了。有没有其他办法呢楼主去看下css吧。。这个是相对于他的容器绝对定位,不是相对于整个视窗 可是只是这样设置的话,x的图片变成绝对定位了,这样不好,万一窗口变小布局整个都不行了。有没有其他办法呢楼主去看下css吧。。这个是相对于他的容器绝对定位,不是相对于整个视窗恩 这个方法可以有,已经试过可以了。只要在父层元素加个布局就行了。3q 感觉就是这个,之前是不显示,输入错误后,再display form表单的问题 急急!在线等,JS中如何有效过滤内容中所有的HTML标签、CSS样式表、JS代码 !求教 AJAX 有时会出现不连接的情况 js如何实现一个类的继承加多态? javascript调用oxc控件,怎么判断oxc控件注册是否成功,非常非常重要的一个问题 没分了。大侠还是要帮忙亚。js如何取得网址中传递的变量的值呀? 如何动态合并html table的列? 想知道xmlhttp实现异步传输的原理是什么? 如何在网页中嵌入一个功能可以定制的文本编辑器? 服务器端base64编码,js base64解码。还有少量中文乱码。 关于自动跳转页面 linux中斜线不代表根路径吗?
设背景图片,这个主意可以试一下,那下面淘宝的做法能说详细点吗?css我也是菜鸟
<input type="text" style="width:120px"/>
<img src="x的图片地址" style="position:absolute;left:110px;top:0px" alt="x"/>
</div>楼主还是去看下css,要不给你代码你也搞不懂
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;public class VerifiCodeAction extends BaseAction {
private InputStream imgCodeStream; private String verifiCode; private boolean ok = false; private static int WIDTH = 80; private static int HEIGHT = 30; private static int NUM = 5; private static char[] seq = { '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' }; public InputStream getImgCodeStream() {
return imgCodeStream;
} public void setImgCodeStream(InputStream imgCodeStream) {
this.imgCodeStream = imgCodeStream;
} public boolean isOk() {
return ok;
} public void setOk(boolean ok) {
this.ok = ok;
} public String getVerifiCode() {
return verifiCode;
} public void setVerifiCode(String verifiCode) {
this.verifiCode = verifiCode;
} public String code() throws Exception {
String code = imageCode();
// 把code放入Session中
session.put(KEY_VERIFICODE, code); System.out.println(code);
return "success";
} public String verify() throws Exception {
if (verifiCode != null && verifiCode.length() > 0) {
String code = (String) session.get(KEY_VERIFICODE);
if (verifiCode.equals(code)) {
ok = true;
}
}
return "success";
} private String imageCode() throws ImageFormatException, IOException {
Random r = new Random(); // 图片的内存映像
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB); // 获得画笔对象
Graphics g = image.getGraphics();
g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
g.fillRect(0, 0, WIDTH, HEIGHT);
g.setColor(new Color(0, 0, 0)); // 用于存储随机生成的验证码
StringBuffer number = new StringBuffer(); // 绘制验证码
for (int i = 0; i < NUM; i++) {
g.setColor(new Color(r.nextInt(10), r.nextInt(10), r.nextInt(10)));
int h = (int) ((HEIGHT * 20 / 100) * r.nextDouble() + (HEIGHT * 80 / 100));
g.setFont(new Font(null, Font.BOLD | Font.ITALIC, h));
String ch = String.valueOf(seq[r.nextInt(seq.length)]);
number.append(ch);
g.drawString(ch, i * WIDTH / NUM * 90 / 100, h);
} //绘制干扰线
// for (int i = 0; i <= 12; i++) {
// g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
// g.drawLine(r.nextInt(WIDTH), r.nextInt(HEIGHT), r.nextInt(WIDTH), r
// .nextInt(HEIGHT));
// } ByteArrayOutputStream baos = new ByteArrayOutputStream();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);
encoder.encode(image); imgCodeStream = new ByteArrayInputStream(baos.toByteArray()); return number.toString();
}
}
<img src="verificode.action"
onclick="this.src='verificode.action?'+(new Date()).getTime()" />Js:
//判断必须填写内容
$.fn.required = function (msg, msgCnt) {
var value = this.val();
if (value != null && value.length > 0) {
$(msgCnt).text("");
return true;
} else {
$(msgCnt).text(msg);
return false;
}
};jsp:
$(function() {
$("#submit").click(function() {
var b1 = $("#user").required("用户名必须填写",$("#msg_user"));
var b2 = $("#pwd").minLength(3,"密码长度必须大于等于3位",$("#msg_pwd"));
var b3 = $("#verificode").required("验证码必须填写",$("#msg_verificode"));
var b4 = false;
if(b3) {
b4 = $("#verificode").remote("verify.action","验证码错误",$("#msg_verificode"));
}
if(b1 && b2 && b4) {
$("#loginForm").submit();
}
});
});
只要在父层元素加个布局就行了。3q