<%@ page language="java" import="java.util.*" contentType="text/html;charset=GBK"%>
<%
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>My JSP 'LoginIn.jsp' starting page</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">
function checkLoginId(){
var loginId=document.form1.txtLoginId.value;
if(loginId.length==0){
alert("请输入用户名");
return false;
}else{
reruen true;
}
}

function checkPassword(){
var Password=document.form1.txtPassword.value;
if(Password.length==0){
alert("请输入密码");
return false;
}else{
return true;
}
}

function checkSub(){
if(checkLoginId() && checkPassword()){
return true;
}else{
return false;
}
}

</script>


  </head>
  
  <body background="file:///E|/workspace/BookNet/WebRoot/images/background.jpg">
  <form name="form1" method="post" action="">
    <p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<table>
<tr>
<td width="275px" height="206px">
</td>
<td>
<img src="file:///E|/workspace/BookNet/WebRoot/images/DSCN0972.JPG"> 
</td>
<td>
用户名:<input type="text" name="txtLoginId"><br>
<br><br>
密  码: <input type="text" name="txtPassword"><br>
<br><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" name="subLogin" value="登录" onClick="return checkSub()">&nbsp;&nbsp;&nbsp;&nbsp;
<input type="button" name="btnRegister" value="注册">
</td>
</tr>
</table></form>
  </body>
</html>
我加了javascript想验证非空输入,就是提交是如果“用户名”和“密码”没有写的话会谈出警告
但是为什么我现在就算不填,也不会谈出来警告也能提交
谢谢!

解决方案 »

  1.   

    你调用事件调错了,应该写在表单里<form name="form1"  onsubmit="return checkSub() >
    function checkSub(){ 
    if(checkLoginId() && checkPassword()){ 
    return true; 
    }else{ 
    return false; 


    这个函数并没有做提交表单的处理,建议写验证写在一个函数里,如果你是
    想用普通的单击事件提交表单的话,就应该这样写
    function check(){
    var username=document.form1.txtLoginId;
    var password=docuemnt.form1.txtPassword;
    if(username.value==''){
    alert('登陆名不能为空');
     username.focus();
    return
    }
    <input type="button"  value="登录" onClick="check()">
    if(password.value==''){
    alert('密码不能为空');
       password.focus();
    return;
    }
    document.form1.submit();
    }
    在<body>处添加:
      

  2.   

    <input type="button"  value="登录" onClick="check()">
      

  3.   

    <input type="submit" name="subLogin" value="登录" onClick="return checkSub()">关键是submit,不关怎么样,只要你点击了他就会提交到你指定的页面