在注册或者录入信息时,需要检查用户名是否存在,不需要点击提交按钮的时候才能查看,在鼠标移开用户名文本框的时候就可以显示,下面给出我做的程序
主要代码红色标示
1、显示页面 的相关代码
<head>
<script type="text/javascript">
var req;
function validate(){
var id=document.getElementById("s1.sp_id").value;
var url="validate.jsp?id="+id;
// alert("validate()");
if(window.XMLHttpRequest){
req=new XMLHttpRequest();
}else if(window.ActiveXObject){
req=new ActiveXObject("Microsoft.XMLHttp");
}
req.open("GET",url,true);
req.onreadystatechange=callback;
req.send(null);
}
function callback(){
if(req.readyState==4){
if(req.status==200){
document.getElementById("idmsg").innerHTML=req.responseText;
// alert("done");
//var msg=req.responseXML.getElementByTagName("msg")[0];
//setMsg(msg.childNodes[0].nodeValue);
}
else{
alert("wrong");
}
}
}
</script>
</head>
<body class="page-header-fixed">
......
<legend class="text01"></legend>
<p class="text02">供商注册号:
<input name="sp_id" type="text" id="s1.sp_id" size="18" οnblur="validate()">
<span id="idmsg"></span>
</p>
..............
2、验证页面 validate.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"
import="edu.sdkj.connection.MysqlUtil"
import=" edu.sdkj.model.supplier"
import="edu.sdkj.util.StringUtil"
import="java.sql.ResultSet;"%>
<html>
<body>
<%
int id=Integer.parseInt(request.getParameter("id"));
System.out.println(id);
try {
String selectsql ="select * from supplierinfo where sp_id='"+id+"' ";
MysqlUtil util = new MysqlUtil(selectsql);
ResultSet rs = util.executeQuery();
if(rs.next()){
response.getWriter().write("工商号已存在");
} else {
response.getWriter().write("工商号成功录入");
}
} catch (Exception e) {
e.printStackTrace();
}
%>
希望对大家有帮助