Ajax验证用户名是否存在

ld191474639 2012-05-03 03:02:07
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript" src="js/jquery-1.6.4.js"></script>
<script language="javascript">
function checkname(uname){

var us=document.getElementById("us");
var name=trim(uname);

if(name==null||name==""){
us.innerHTML="<font color='red'>用户名不能为空</font>"
return false;
}else if(!/^[a-zA-Z]\w{0,9}$/.test(name)){
us.innerHTML="<font color='red'>用户名开头必须为字母</font>"
return false;
}
else{
us.innerHTML="";
return true;
}

}
function trim(s1){
return s1.replace(/^\s+|\s+$/g,"");//去除空格;

}
function check(){//Ajax
var z=$("#name").val();
$.post("check",{uname:z},function (data){
if(data=="用户名已经存在"){
$("#us").html("<font color='red'>"+data+"</font>");
return false;
}else{

return true;

}
})
}
function checkdata(){
var name=document.getElementById("name");
var pass=document.getElementById("password");
var pass2=document.getElementById("pass");
var email=document.getElementById("email");
if(!checkname(name.value)){
return false;
}
if(!check())
{return false;

}
if($("#us").text=="用户名已经存在"){
return false;
}
$("#my").submit();
return true;
}
</script>
<base href="<%=basePath%>">

<title>My JSP 'MyJsp.jsp' starting page</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="index.jsp" method="post" id="my">
<table border="0" cellspacing="1" cellpadding="5">
<tr><td>用户名</td>
<td><input type="text" id="name" onblur="checkname(this.value)" name="uname"/></td><td><span id="us">
</span></td></tr>

</table>
<input type="submit" value="注册" onclick="return checkdata()"/>       <input type="submit" value="取消"/>
</form>
</body>
</html>

我的servlet
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
String uname=request.getParameter("uname");
PrintWriter out=response.getWriter();
if(uname.equals("dada")){
out.print("用户名已经存在");
}
out.flush();
out.close();
}

现在是输入不存在的用户名,页面不能提交,怎么可以使页面提交
...全文
263 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ebonyzhang 2012-05-08
  • 打赏
  • 举报
回复

function check(uname){
var us=document.getElementById("us");
var name=trim(uname);
if(name==null||name==""){
us.innerHTML="<font color='red'>用户名不能为空</font>";
return false;
}else if(!/^[a-zA-Z]\w{0,9}$/.test(name)){
us.innerHTML="<font color='red'>用户名开头必须为字母</font>";
return false;
}else{
us.innerHTML="";
return true;
}
var z=$("#name").val();
$.post("check",{uname:z},function (data){
if(data=="用户名已经存在"){
$("#us").html("<font color='red'>"+data+"</font>");
return false;
}else{
return true;
}
});
$("#my").submit();
}


ebonyzhang 2012-05-08
  • 打赏
  • 举报
回复
提交的部分,应该在注册按钮那儿!
.云哲. 2012-05-03
  • 打赏
  • 举报
回复
建议楼主调试Ajax使用火狐firebug调试,监听Ajax请求和相应过程。
ld191474639 2012-05-03
  • 打赏
  • 举报
回复
不行,还是给提交了。
三石-gary 2012-05-03
  • 打赏
  • 举报
回复
if(check()=='false')
{
return false;
}

else{return true;
}
这样呢?
ld191474639 2012-05-03
  • 打赏
  • 举报
回复
现在问题是如果用户名存在,也给提交了。
ld191474639 2012-05-03
  • 打赏
  • 举报
回复
改成if(check())
{
return false;

}
直接给提交了,网页出现了一个警示框stack out inline 145;
001007009 2012-05-03
  • 打赏
  • 举报
回复
好混乱,楼主 把jquery 的方法 和 js原生方法 用得够混的。
三石-gary 2012-05-03
  • 打赏
  • 举报
回复
if(!check())
{
return false;

}

觉得这句话有问题。。改为
if(check())
{
return false;

}
试试

52,797

社区成员

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

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