<html>
<head>
<title>HTML注册表单</title>
<script type="text/javascript">
function sub(){
var f = document.form1//取form对象
var userName = f.userName.value//取用户名
var passWord = f.passWord.value//取密码
var repassWord = f.repassWord.value//取确认密码
var box1 = f.theRead.checked//判断复选框是否被选中
var box2 = f.theTravel.checked//判断复选框是否被选中
var box3 = f.theInt.checked//判断复选框是否被选中
var count = (box1 ? 1 : 0) + (box2 ? 1 : 0) + (box3 ? 1 : 0)//判断复选框被选中几个
var readme = f.readme.value//取自我介绍内容
var rename = new RegExp("^[a-zA-Z_][a-zA-Z_0-9]{0,}", "")//匹配用户名的正则表达式
var repass = new RegExp("[a-zA-Z_0-9]{6,16}", "")//匹配密码的正则表达式
if(userName == ""){
alert("用户名不能为空!")
return false
}else if(rename.test(userName) == false){
alert("用户名: 必须是字母数字或下划线, 不能以数字开头")
return false
}else{
if(passWord == ""){
alert("用户密码不能为空!")
return false
}else if(repass.test(passWord) == false){
alert("密码: 必须是6-16位字母数字下划线!")
return false
}else{
if(repassWord == ""){
alert("请输入确认密码!")
return false
}else if(passWord != repassWord){
alert("两次输入的密码不一致!")
return false
}else{
if(count <1){
alert("请选择至少一项兴趣爱好!")
return false
}else{
if(readme == ""){
alert("请填写个人介绍!")
return false
}
}
}
}
}
}
</script>
</head>
<body>
<form name="form1" id="form1" action="" method="post" onsubmit="return sub()">
用 户 名:<input type="text" name="userName" id="userName" value=""/>
<br>
用户密码:<input type="password" name="passWord" value=""/>
<br>
确认密码:<input type="password" name="repassWord" value=""/>
<br>
性别:
<input type="radio" name="theSex" value="0" checked=”default” />男
<input type="radio" name="theSex" value="1" />女
<br>
城市:
<select name="theCity">
<option value="0">郑州</option>
<option value="1">上海</option>
<option value="2">天津</option>
<option value="3">深圳</option>
</select>
<br>
兴趣爱好:
<input type="checkbox" name="theRead" value="0" />阅读
<input type="checkbox" name="theTravel" value="1" />旅游
<input type="checkbox" name="theInt" value="2" />上网
<br>
个人简介:
<br>
<textarea rows="5" cols="30" name="readme"></textarea>
<br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
我这边写项目都是是后台写的jsp代码,但是前端也是可以实现的,用户登录之后,将用户名localStorage.setItem在本地保存一下,然后localStorage.getItem判断本地的用户名是不是存在的,存在显示登录后页面,不存在的话就显示登录注册。退出登录的时候可以让用户名位空或者是用localStorage.removeItem清除本地用户名的缓存。希望有用。不到之处务喷jsp代码<%@ page language="java" pageEncoding="GBK"%>
<%
response.setContentType("text/xmlcharset=gbk")
response.setHeader("Cache-Control","no-cache")
String username=request.getParameter("username")
boolean isValid=false
if(username.equals("admin")){
isValid=true
}
if(isValid){
out.println("<content>该用户名已经存在!</content>")
}
else{
out.println("<content>ok</content>")
}
%>
------------------------
html代码
<html>
<head>
<title></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="">
<meta http-equiv="description" content="">
<meta http-equiv="Content-Type" content="text/htmlcharset=gb2312">
<script>
var XMLHttpReq=false
function createXMLHttpRequest(){
if(window.XMLHttpRequest){
XMLHttpReq=new XMLHttpRequest()
}
else if(window.ActiveXObject){
try{
XMLHttpReq=new ActiveXObject("Msxml2.XMLHTTP")
}catch(e){
try{XMLHttpReq=new ActiveXObject("Microsoft.XMLHTTP")}catch(e){}
}
}
}
function send(url){
//alert(url)
createXMLHttpRequest()
XMLHttpReq.open("GET",url,true)
XMLHttpReq.onreadystatechange=parse
XMLHttpReq.send(null)
}
function parse(){
if(XMLHttpReq.readyState==4){
if(XMLHttpReq.status==200){
var doc = new ActiveXObject("MSxml2.DOMDocument")
doc.loadXML(XMLHttpReq.responseText)
var res=doc.getElementsByTagName("content")[0].firstChild.data
if(res=="ok"){
document.getElementById("status").innerHTML="该用户名可以使用!"
}
else{document.getElementById("status").innerHTML=res
window.alert(res)}
}else{window.alert("所请求的页面异常!")}
}
}
function checkUsername(){
var username=document.getElementById("username").value
if(username==""){
alert("请输入用户名!")
return false
}else{
send('test.jsp?username='+username)
}
}
</script>
</head>
<body bgcolor="#9393FF">
<table height="100%" width="100%">
<tr>
<td align="center">
<table >
<tr>
<td width="10%"></td>
<td>
<form name="form1" >
<table width="300" boder="0">
<tr>
<td colspan="2">
<div id="status"><font size=6>注册新用户</font></div>
</td>
<tr>
<td><font size=5>*用户名</font></td>
<td><input type="text" name="username" size=15 maxlength="10" value="" onblur="checkUsername()" ></td>
</tr>
<tr>
<td><font size=5>*密码</font></td>
<td><input type="password" name="password1" ></td>
</tr>
<tr>
<td><font size=5>*确认密码</font></td>
<td><input type="password" name="password2" ></td>
</tr>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
jsp、和html都做过简单修改,没什么大毛病,需要你细心。 此处的jsp名称换成你自己的send('test.jsp?username='+username)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)