<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import = "java.util.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录操作</title>
</head>
<body>
<script language="javascript">
function validate(f){
if(!(/^\w{1,9}$/.test(f.id.value))){
document.getElementById("idmsg").innerHTML = "用户名必须是1~9位!";
f.userid.focus() ;
return false ;
}
if(!(/^\w{1,9}$/.test(f.password.value))){
alert("密码必须是1~9位!") ;
f.userpass.focus() ;
return false ;
}
}
</script>
<%
request.setCharacterEncoding("GBK") ;
%>
<%
Map<String,String> message = (HashMap<String,String>) request.getAttribute("message") ;
if(message != null){ // 有信息返回
Set<String> keySet = message.keySet() ;
Iterator<String> iter = keySet.iterator() ;
while(iter.hasNext()){
%>
<h4><%=message.get(iter.next())%></h4>
<%
}
}
%>
<center>
<form action = "LoginServlet" method = "post" onSubmit="return validate(this)">
<h1>用户登录</h1>
<table border = "1">
<tr>
<td>用户ID:</td>
<td><input type = "text" name = "id"></input><span id = "idmsg"></span></td>
</tr>
<tr>
<td>密码:</td>
<td><input type = "password" name = "password"></input><span id = "passwordmsg"></span></td>
</tr>
<tr>
<td colspan = "2"><input type = "submit" value = "登陆">&nbsp;<input type = "reset" value = "重置"></td>
</tr>
</table>
</form>
</center>
</body>
</html>javascript表单

解决方案 »

  1.   

    补充两张图第一张为登陆输入参数点登录后出现过“用户名必须是1~9位!”说明js起作用了,应该返回false了,但很快就被提交到LoginServlet,如下图
      

  2.   

    document.getElementById("idmsg").innerHTML = "用户名必须是1~9位!";
    f.userid.focus() ;     //你保证这里正确执行了?
    return false ;
      

  3.   

    知道了,f.userid.focus();这句话错了,应该是f.id.focus();谢谢了