日期:2014-05-16 浏览次数:20587 次
<%@ 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>
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(); }