jsp+Ajax判断邮箱、用户名是否存在的问题(急) - Web 开发 / Ajax
<HTML><HEAD><TITLE></TITLE></HEAD><script type="text/javascript" language="javascript">var xmlHttp=false;var ShowObj="";function AjaxCheck(url,httpMethod,Show_Obj){ ShowObj=Show_Obj; if(!httpMethod) { httpMethod="POST"; } xmlHttp=CreateXmlRequest(); xmlHttp.onreadystatechange=StateContent; xmlHttp.open(httpMethod,url,true); xmlHttp.send(null);}function StateContent(){ if(xmlHttp.readyState==4) { if(xmlHttp.status==200) { MakeOperate(); } }}function MakeOperate(){ ShowObj.className = "Erro_Css"; //ShowObj.innerHTML = xmlHttp.responsetext; ShowObj.innerHTML = xmlHttp.responseText;}</script><script type="text/javascript" language="javascript">function CreateXmlRequest( ){ var xmlhttp_request = false; try { if( window.ActiveXObject ) { for( var i = 5; i; i-- ) { try { if( i == 2 ) { xmlhttp_request = new ActiveXObject("Microsoft.XMLHTTP"); } else { xmlhttp_request = new ActiveXObject("Msxml2.XMLHTTP." + i + ".0"); xmlhttp_request.setRequestHeader("Content-Type","text/xml"); xmlhttp_request.setRequestHeader("Content-Type","UTF-8"); } break; } catch(e) { xmlhttp_request = false; } } } else if(window.XMLHttpRequest) { xmlhttp_request = new XMLHttpRequest(); if (xmlhttp_request.overrideMimeType) { xmlhttp_request.overrideMimeType('text/xml'); } } } catch(e) { xmlhttp_request = false; } return xmlhttp_request ;}</script><script language="javascript">function checkspace(checkstr){ var str = ''; for(i = 0; i < checkstr.length; i++) { str = str + ' '; } return (str == checkstr);}function getObj(objName){ return(document.getElementById(objName));}function Check_OK(Erro_ID){ var StrErro; var Obj; Obj=getObj(Erro_ID); StrErro = "<table width='300' bgcolor=E2F5FF><tr><td align=left valign=middle class=tdborder> <font color=red><B>ok!</B></font></td></tr></table>"; Obj.innerHTML = StrErro;}function Check_AssetOK(Erro_ID){ var StrErro; var Obj; Obj=getObj(Erro_ID); StrErro = "<FONT COLOR=RED font-size:14px>资料载入中,请稍后...</FONT>"; Obj.innerHTML = StrErro;}function Show_Erro(Obj,Erro_Info){ Obj.innerHTML = Erro_Info;}function Check_Onfocus(Erro_ID,Words){ var Obj; Obj=getObj(Erro_ID); Obj.innerHTML = Words;}//Email验证function Check_internetadd(Input_Value,Erro_ID){ var StrErro; var Obj=getObj(Erro_ID); if (Input_Value.length==0||checkspace(Input_Value)) { StrErro = '<table width =300 bgcolor="#FFE2E9"><tr><td class=tdborder4 ><font color=red> 您输入的邮件地址有误,邮件地址不能为空。</font></td></tr></table>'; Show_Erro(Obj,StrErro); return false; } var patn = /^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/; if(!patn.test(Input_Value)) { var StrErro = '<table width=300 bgcolor=#FFE2E9><tr><td align=left valign=middle class=tdborder4> <font color=red>请输入正确的邮件地址。</font></td></tr></table>'; Show_Erro(Obj,StrErro); return false; } else { AjaxCheck("Job/Check_internet_add.jsp?internet_add="+Input_Value,"",Obj); } Check_AssetOK(Erro_ID); return true;}//用户名验证function Check_userlogin(Input_Value,Erro_ID){ var StrErro; var Obj=getObj(Erro_ID); if (Input_Value.length==0||checkspace(Input_Value)) { StrErro = '<table width="300" bgcolor="#FFE2E9"><tr><td class=tdborder4 ><font color=red> 您好,用户名不能为空!</font></td></tr></table>'; Show_Erro(Obj,StrErro); return false; } //长度验证 if(Input_Value.length<6||Input_Value.length>16) { SrtErro = '<table width="300" bgcolor="#FFE2E9"><tr><td class=tdborder4 ><font color=red> 长度不正确,由6-16位英文字母、数字、下划线组成。</font></td></tr></table>' Show_Erro(Obj,SrtErro); return false; } var patn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){5,15}$/; //匹配IP地址的正则表达式 if(!patn.test(Input_Value)) { StrErro = '<table width="300" bgcolor="#FFE2E9"><tr><td class=tdborder4 ><font color=red> 以字母开头由6-16位英文字母、数字、下划线组成。</font></td></tr></table>'; Show_Erro(Obj,StrErro); return false; } else { AjaxCheck("Job/Check_userlogin.jsp?user_login="+Input_Value,"",Obj); } Check_AssetOK(Erro_ID); return true;}//统一验证function checkdata(){ var pass = true; if(!Check_internetadd(document.form1.internetadd.value,"Check")){ pass = false; } if(!Check_userlogin(document.form1.userlogin.value,"Check1")){ pass = false; } return pass;}</script><BODY topmargin="0" rightmargin="10" leftmargin="10"><table border="0" cellpadding="0" cellspacing="0" height="407" width="720" style="margin-left:30px;margin-top:40px " > <form name="form1" method="post" action="Job/StellaReg_ok.jsp" onsubmit="return checkdata();"> <tr> <td width="100" height="40" align="right"><strong>电子邮件:</strong></td> <td width="180" height="40" ><input type="text" size="30" name="internetadd" id="internetadd" onFocus="Check_Onfocus('check','<table width=100% bgcolor=E2F5FF><tr><td align=left valign=middle class=tdborder> 请准确填写您的E-MAIL地址。</td></tr></table>')" onBlur="Check_internetadd(this.value,'Check');"></td> <td width="300" id="Check" height="40" > <table width="300" height="40" > <tr><td align=left valign=middle width="300" ><div style="width:300px;border:1px solid #CCCCCC;line-height:25px "> (请准确填写您的E-MAIL地址。) </div> </td></tr></table> </td> </tr> <tr> <td width="100" height="40" align="right"><strong>用 户 名:</strong></td> <td width="180" height="40" > <input type="text" size="30" name="userlogin" id="userlogin" onFocus="Check_Onfocus('check1','<table width=100% bgcolor=E2F5FF><tr><td align=left valign=middle class=tdborder> 用户名必须为英文字母、数字、下划线或中文组成。</td></tr></table>')" onBlur="Check_userlogin(this.value,'Check1');"> </td> <td width="300" id="Check1" height="40" > <table width="300" height="40"> <tr><td align=left valign=middle width="300"><div style="width:300px;border:1px solid #CCCCCC;line-height:25px "> (以字母开头由6-16位英文字母、数字、下划线组成。) </div> </td></tr></table> </td> </tr><tr> <td width="100" height="30" align="right"> </td> <td width="180"><input type="submit" value="确认" name="b1" ></td> <td width="200"> </td> </tr> </form> </table> </BODY></HTML>
<HTML><%String internetadd=""; internetadd=request.getParameter("internet_add"); %><HEAD><TITLE></TITLE></HEAD><jsp:useBean id="OracleConn" scope="application" class="stella.eip.OracleConn"/><body> <% Connection conn=null; Statement stmt=null; ResultSet rs=null; String sql=null; try{ conn=OracleConn.getConnection(); stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); sql="select * from EIP_USERS where internetadd='"+internetadd+"' "; rs=stmt.executeQuery(sql); if(rs.next()){ out.println("<table width=300 bgcolor=#FFE2E9><tr><td align=left valign=middle class=tdborder><font color='red'><b>"+internetadd+"</b></font>已经存在.</td></tr></table>"); } else { out.println("<table width=300 bgcolor=E2F5FF><tr><td align=left valign=middle class=tdborder><font color='red'><b>"+internetadd+"</b></font>可以使用.</td></tr></table>"); } rs.close(); }catch(Exception sqle){ out.print(sqle);} finally { if (stmt != null)stmt.close(); if (conn != null)conn.close(); } %> </body></HTML>
function AjaxCheck(url,httpMethod,Show_Obj){ ShowObj=Show_Obj; if(!httpMethod) { httpMethod="POST"; } xmlHttp=CreateXmlRequest(); //xmlHttp.onreadystatechange=StateContent; xmlHttp.open(httpMethod,url,/*true*/false);//改为同步,这样不需要状态转换函数了 xmlHttp.send(null);ShowObj.className = "Erro_Css"; //ShowObj.innerHTML = xmlHttp.responsetext; ShowObj.innerHTML = xmlHttp.responseText;//你返回了字符串,这样非常不明智的返回,应该直接返回true或者false,那么只能判断“可以使用”或者“已经存在”了if(xmlHttp.responseText.indexOf("可以使用")!=-1)return true;//else return false;}function Check_userlogin(Input_Value,Erro_ID){ var StrErro; var Obj=getObj(Erro_ID); if (Input_Value.length==0||checkspace(Input_Value)) { StrErro = '<table width="300" bgcolor="#FFE2E9"><tr><td class=tdborder4 ><font color=red> 您好,用户名不能为空!</font></td></tr></table>'; Show_Erro(Obj,StrErro); return false; } //长度验证 if(Input_Value.length<6||Input_Value.length>16) { SrtErro = '<table width="300" bgcolor="#FFE2E9"><tr><td class=tdborder4 ><font color=red> 长度不正确,由6-16位英文字母、数字、下划线组成。</font></td></tr></table>' Show_Erro(Obj,SrtErro); return false; } var patn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){5,15}$/; //匹配IP地址的正则表达式 if(!patn.test(Input_Value)) { StrErro = '<table width="300" bgcolor="#FFE2E9"><tr><td class=tdborder4 ><font color=red> 以字母开头由6-16位英文字母、数字、下划线组成。</font></td></tr></table>'; Show_Erro(Obj,StrErro); return false; } else { Check_AssetOK(Erro_ID); return AjaxCheck("Job/Check_userlogin.jsp?user_login="+Input_Value,"",Obj);//========使用了不同,所以直接返回ajax的验证结果 } //Check_AssetOK(Erro_ID); //return true;}//统一验证
[解决办法]
是啊 异步返回数据问题 改为同步