function checkUser(objName){ if(objName==''){ $('.redNote').text('用户名不能为空!'); } else{ $.post('checkTea.asp',{username:objName, rnd:new Date().getTime()},function(msg){ $('.redNote').text(msg.info); },'json'); } } function checkForm(){ if(document.form1.username.value==''){ alert("请输入用户名!"); document.form1.username.focus(); return false; } else{ var result=0; $.ajax({async:false, //同步通信 type:'POST', dataType:'json', url:'checkTea.asp', data:{username:$('#username').val(),rnd:new Date().getTime()}, success:function(msg){ if(msg.errChar==1){ alert(msg.info); result=1; } } }); if(result==1){return false;} //不在回调函数内判断用户名是否被占用 } if(document.form1.userpass.value==''){ alert("请输入密码!"); document.form1.userpass.focus(); return false; } if(document.form1.formUserpass.value==''){ alert("请再次输入密码!"); document.form1.formUserpass.focus(); return false; } if(document.form1.userpass.value!=document.form1.formUserpass.value){ alert("两次密码不一致,请重新输入!"); document.form1.userpass.value=''; document.form1.formUserpass.value=''; return false; } if(document.form1.realname.value==''){ alert("姓名不能为空!"); document.form1.realname.focus(); return false; } var s=false,n=0; var grade,subject; for(var i=0;i<document.getElementsByName("grade").length;i++){ if(document.getElementsByName("grade")[i].checked){ s=true; n+=1; (n==1)?grade=document.getElementsByName("grade")[i].value:grade+=','+document.getElementsByName("grade")[i].value; } } if(!s){ alert("请选择您擅长的年级!"); return false; } s=false;n=0; //重置flag变量 for(var i=0;i<document.getElementsByName("subject").length;i++){ if(document.getElementsByName("subject")[i].checked){ s=true; n+=1; (n==1)?subject=document.getElementsByName("subject")[i].value:subject+=','+document.getElementsByName("subject")[i].value; } } if(!s){ alert("请选择您擅长的科目!"); return false; } if(document.form1.addr.value==''){ alert("地址不能为空!"); document.form1.addr.focus(); return false; } if(document.form1.tel.value==''){ alert("电话不能为空!"); document.form1.tel.focus(); return false; } //return true; //采用post方式提交数据 $.post('saveReg.asp',{username:$('#username').val() ,userpass:$('#userpass').val() ,realname:$('#realname').val() ,school:$('#school').val() ,degree:$('#degree').val() ,grade:grade ,subject:subject ,addr:$('#addr').val() ,tel:$('#tel').val() ,email:$('#email').val() ,rnd:new Date().getTime()},function(msg){ alert(msg); location.href='default.asp'; }); } $(function(){ $('#form1').submit(function(){ checkForm(); return false; //阻止默认的form提交 }); });
后台处理页面代码:
<!--#include file="conn.asp"--> <!--#include file="inc/md5.asp"--> dim username,userpass,realname,school,degree dim grade,subject,addr,tel,email,classId classId=Int(Request.Form("classId")) username=Trim(Request.Form("username")) userpass=md5(Trim(Request.Form("userpass"))) realname=Trim(Request.Form("realname")) school=Trim(Request.Form("school")) degree=Request.Form("degree") grade=Trim(Request.Form("grade")) subject=Trim(Request.Form("subList")) addr=Request.Form("addr") tel=Trim(Request.Form("tel")) email=Trim(Request.Form("email")) dim rs,sql if classId=1 then '---------------------------------------- '判断用户名唯一 '---------------------------------------- set rs = Server.CreateObject("ADODB.Recordset") rs.open "select id from Teacher where username='"&username&"' OR tel='"&tel&"'",conn,1,1 if not rs.eof then Response.Write("{errCode:1,errChar:'用户名或手机电话被占用!'}") Response.End() end if rs.close set rs=Nothing sql="select * from Teacher where id is null" set rs = Server.CreateObject("ADODB.Recordset") rs.Open sql,conn,1,3 rs.addnew rs("username")=username rs("userpass")=userpass rs("realname")=realname rs("school")=school rs("degree")=degree rs("grade")=Request.Form("gradeList") rs("subject")=subject rs("addr")=addr rs("tel")=tel rs("email")=email rs.update rs.close set rs=nothing elseif classId=2 Then '---------------------------------------- '判断用户名或手机唯一 '---------------------------------------- set rs = Server.CreateObject("ADODB.Recordset") rs.open "select id from [Student] where username='"&username&"' OR tel='"&tel&"'",conn,1,1 if not rs.eof then Response.Write("{errCode:1,errChar:'用户名或手机电话被占用!'}") Response.End() end if rs.close set rs=Nothing sql="select * from Student where id is null" set rs = Server.CreateObject("ADODB.Recordset") rs.Open sql,conn,1,3 rs.addnew rs("username")=username rs("userpass")=userpass rs("realname")=realname rs("school")=school rs("grade")=grade rs("subject")=subject rs("tel")=tel rs("email")=email rs.update rs.close set rs=nothing end if conn.Close set conn=Nothing Response.Write("{errCode:0,info:'注册成功!'}")