求jsp高手帮忙,我要实现的是一个用户登录界面,用了JavaScript来进行相关的验证,如用户名不能为空,但在浏览器不能实现相关的验证功能,求高手,下面是我的代码,
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title><style type="text/css">
<!--
.STYLE1 {font-size: 24px}
-->
</style><script language="javascript">
function check()
{ var x=document.getElementById("username").value;
var y= document.getElementById("password").value;
if(x==""&&x===null){alert("请输入用户名")
document.getElementById("username").focus();
return false;}
if(y==""&&y===null){alert("请输入密码")
document.getElementById("password").focus();
return false;}
}</script>
</head><body>
<form id="form1" name="form1" method="post" action=""  onsubmit="javascript:return check()" >
  <table width="221" border="0" align="center" bgcolor="#00FF00">
    <tr>
      <td width="211" height="17" align="center"><span class="STYLE1">用户登录</span></td>
    </tr>
    <tr>
      <td height="25" nowrap="nowrap"><span class="STYLE1">用户名:</span>      
      <input name="username" type="text" size="23" /></td>
    </tr>
    <tr>
      <td height="30"><span class="STYLE1">密 码:</span> 
      <input name="password" type="password" size="23" /></td>
    </tr>
    <tr>
      <td height="17" align="center"><input type="submit" name="Submit" value="登录"  />
      <input type="reset" name="Submit2" value="重置" /></td>
    </tr>
    <tr>
      <td height="26">&nbsp;</td>
    </tr>
  </table>
</form>
  
</body>
</html>

解决方案 »

  1.   

    input请加上ID<input name="username" id="username" type="text" size="23" />
      

  2.   

    getElementById(xxx)是获取id为xxx的元素。在IE中也可以获取name为xxx的元素,但在其他浏览器不行
      

  3.   

    if(x==""&&x==null)是或者 ||   不是并且  &&还有顺序换一下if(x==null ||x=="")
      

  4.   

    <input name="username" id="username" type="text" size="23" />
    <input name="password"  id="password" type="password" size="23" />
    首先 
      

  5.   

    页面没有username和password的id。。加上。
      

  6.   

    x==""&&x===null判断这里也有问题 。
      

  7.   

    if(x==""&&x===null){alert("请输入用户名") 永远不会执行document.getElementById("username")是by ID  你有 id="username" 的控件?
      

  8.   

    问题这么多,就先用JQ的表单验证控件做吧。有时间好好学学JS.