第一个问题 我在注册的时候 如果用户名少于六个字母就提示,就像你注册百度一样 右边有个提示 说您的用户名不能少于六位了 但是我只会些 alert 但是我闲麻烦 还要点,能不能直接在右边显示 能不能给我一个简单的像百度注册页面的例子 就是你什么都不填写 直接点同意一下协议 都会出现红色的提示 .
第二个问题 就是我登陆网站之后在点击其他的导航,跳转的每个导航的页面的右上角或者左上角都有欢迎你+用户名 这个怎么来实现 谢谢大家帮我回答啊 我不会做网站 能否给个例子 或者写下来
第二个问题 就是我登陆网站之后在点击其他的导航,跳转的每个导航的页面的右上角或者左上角都有欢迎你+用户名 这个怎么来实现 谢谢大家帮我回答啊 我不会做网站 能否给个例子 或者写下来
onblur的时候js做判断,如果少于6位,则让div变为显示,并让输入框重新获得焦点。
这是最常见的做法。
另外再加一个JavaScript的验证是吧。
register.jsp:<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ include file="index.jsp" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Register a new user</title>
<script type="text/javascript">
function validateUsername(){
if(document.getElementById("username").value.length<6)
document.getElementById("wrong_username").style.display="block";
}
function validatePassword(){
if(document.getElementById("password").value.length<6)
document.getElementById("wrong_password").style.display="block";
}
function validate(){
if(document.getElementById("username").value.length<6)
document.getElementById("wrong_username").style.display="block";
if(document.getElementById("password").value.length<6)
document.getElementById("wrong_password").style.display="block";
}
</script>
</head>
<body>
<input type="text" name="username" id="username" onblur="validateUsername();"/>
<font color="red" style="display: none;" id="wrong_username">用户名不得低于六位</font>
<input type="password" name="password" id="password" onblur="validatePassword();" />
<font color="red" style="display: none;" id="wrong_password">密码不得低于六位</font>
<br/><input type="button" value="注册" onclick="validate();"/>
</body>
</html>
index.jsp: <body>
XXX,Welcome to our site!<br>
</body>
我不太同意二楼的做法
感觉使用document.getElementById("").innerHTML = "";
来动态指定div或span比较好,提示万一不是一句而是根据不同条件显示不同提示,那么使用innerHTML还是比较灵活的。
把警告的内容放到一个div里面然后默认让它隐藏,
在function中把它作为input失去焦点的事件
这样,当你输入完内容,当鼠标失去焦点时,它就会判断,你的脚本,然后提示警告div中的内容
<%
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>
<base href="<%=basePath%>">
<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="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script language="javascript" type="text/javascript">
var req;
function check() {
//alert(regForm.username.value) ;
var url = "checkUsername.jsp?username="+regForm.username.value ;
try{
req = new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
try{
req = new ActiveXObject('Microsoft.XMLHTTP');
}catch(e){
try{
req = new XMLHttpRequest();
}catch(e){}
}
}
req.onreadystatechange = pess ;
req.open("GET" , url , false) ;
req.send(null) ;
}
function pess() {
if(req.readyState==4) {
if(req.status==200) {
var xmlDoc=req.responseXML.documentElement;
if(xmlDoc.text == "1"){
document.getElementById("test").innerHTML = "该用户名可以使用" ;
document.getElementById("doSubmit").disabled = false;
req = null;
}else{
document.getElementById("test").innerHTML = "用户名已经存在,请换一个" ;
document.regForm.username.select();
document.getElementById("doSubmit").disabled = true;
req = null;
return false;
}
}
}
}
function testName() {
//alert("===start=====") ;
var reg = /^[0-9a-zA-Z]*$/ ;
var s = document.getElementById("username").value;
if(regForm.username.value==""){
document.getElementById("test").innerHTML = "<font color='red'>用户名不能为空!</font>" ;
document.regForm.username.focus();
return false ;
}
if(!reg.test(s)) {
document.getElementById("test").innerHTML = "<font color='red'>请不要输入汉字!</font>" ;
document.regForm.username.focus();
document.getElementById("doSubmit").disabled = true;
return false ;
}
if(s.length<4||s.length>20) {
document.getElementById("test").innerHTML = "<font color='red'>请输入4-20位的字符串!</font>" ;
document.regForm.username.focus();
document.getElementById("doSubmit").disabled = true;
return false ;
}
check() ;
return true;
}
function checkPassword(){
var reg = /^[a-zA-Z0-9]{6,16}$/ ;
var s = regForm.password.value ;
if(regForm.password.value=="") {
document.getElementById("password1").innerHTML = "<font color='red'>密码不能为空!</font>";
document.regForm.password.select();
document.getElementById("doSubmit").disabled = true;
return false ;
}
if(regForm.password.value.length<6) {
document.getElementById("password1").innerHTML = "<font color='red'>您的密码长度不足6!</font>";
document.regForm.password.select();
document.getElementById("doSubmit").disabled = true;
return false ;
}
if(!reg.test(s)) {
document.getElementById("password1").innerHTML = "<font color='red'>密码请输入6到16位的字母或数字组合!</font>";
document.regForm.password.select();
document.getElementById("doSubmit").disabled = true;
return false ;
}
document.getElementById("password1").innerHTML = "";
return true;
}
function checkRepassword(){
if(regForm.repassword.value=="") {
document.getElementById("repassword1").innerHTML = "<font color='red'>密码不能为空!</font>";
document.regForm.repassword.select();
document.getElementById("doSubmit").disabled = true;
return false ;
}
if(regForm.password.value!=regForm.repassword.value) {
document.getElementById("repassword1").innerHTML = "<font color='red'>两次密码输入不一致!</font>" ;
document.regForm.repassword.select();
document.getElementById("doSubmit").disabled = true;
return false ;
}
document.getElementById("repassword1").innerHTML = "";
return true;
}
function checkAll(){
if(testName() && checkPassword() && checkRepassword()){
document.getElementById("doSubmit").disabled = false;
return true;
}else{
return false;
}
}
</script>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center"> </td>
</table>
<form name="regForm" action="RegServlet" method="post" onsubmit="return checkAll()">
<table width="910" border="1" align="center" bordercolor="#409EE3">
<tr>
<td height="28" colspan="2" background="images/TopBg1.jpg" class="con01">只需五秒立即成为今泽天用户:</td>
</tr>
<tr>
<td width="82" height="37" bgcolor="EEF7FF">用户名:</td>
<td width="910" bgcolor="EEF7FF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<td width="18%" height="28"><input name="username" type="text" id="username" size="20" maxlength="20" onblur="checkAll();"/></td>
<td id="test" width="82%" bgcolor="#DFF0FF"><table width="95%" border="1" cellpadding="0" cellspacing="1" bordercolor="#3C9AE0">
<tr>
<td height="27" bgcolor="#CBEDFC"><DIV>
<DIV>4-20 个字符 (仅限大小写字母,数字,下划线等),注册后不可修改。<span class="a_ff0000" color="red">*</span></DIV>
</td>
</tr>
</table>
</td>
</table></td>
</tr>
<tr>
<td height="24" bgcolor="EEF7FF">输入密码:</td>
<td bgcolor="EEF7FF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="18%" height="28"><input type="password" id="userpassword" name="password" size="20" maxlength="20" onblur="checkAll()"/></td>
<td id="password1" width="82%" bgcolor="#DFF0FF"><table width="95%" border="1" cellpadding="0" cellspacing="1" bordercolor="#3C9AE0">
<tr>
<td height="27" bgcolor="#CBEDFC"><DIV>
<DIV>6-20 个字符。<span class="a_ff0000">*</span></DIV>
</DIV>
</td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td height="24" bgcolor="EEF7FF">重复密码:</td>
<td bgcolor="EEF7FF"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="18%" height="28"><input type='password' id="repassword" name="repassword" size=20 maxlength="20" onblur="checkAll()"/></td>
<td width="82%" bgcolor="#DFF0FF"><table width="95%" border="1" cellpadding="0" cellspacing="1" bordercolor="#3C9AE0">
<tr>
<td id="repassword1" height="27" bgcolor="#CBEDFC"><DIV>重复输入一次上面的密码。<span class="a_ff0000">*</span></DIV></td>
</tr>
</table></td>
</tr>
</table></td>
</tr>
<tr>
<td height="27" bgcolor="EEF7FF">电子邮箱</td>
<td bgcolor="EEF7FF"><input name="email" type='text' id="email" size="30" maxlength="30"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="同意以下服务条款,提交注册信息" id="doSubmit"/>
</td>
</tr>
</table>
</form>
</body>
</html>
这个有个问题 就是 我从数据库中查到有想同的用户名之后 就直接显示到密码不能为空 上面都无法填写 不知道为什么 大家用这个代码 然后建个数据库 自己试一试