日期:2014-05-17  浏览次数:20682 次

小白求指教
<script type="text/javascript">
function kkc(){

var biaodan=document.getElementById("biaodan");
var name=document.getElementById("name").value;
var password=document.getElementById("password").value;
var pwd=document.getElementById("pwd").value;
if (""==name){
alert("亲,用户名不能为空哦")
 
 }
  if(""==password){
alert("亲,密码不能为空哦")
 }
if (""==pwd){
alert("亲,确认密码不能为空哦")
}

if (pwd==name){
alert("亲,用户名与密码不能一样哦")
}
  document.biaodan[0].submit();
   
}
  </script>

  </head>
  
  <body>
  <H3 align=center><FONT size=7 face=楷体_GB2312>欢迎您注册本系统,感谢您的到来!</FONT></H3>
  <form action="t1" id="biaodan" name="biaodan" method="get">
   
  请输入用户名<br><input type="text" id="name" name="name" />
  <br> 
   
  请输入您密码 <br><input type="text" id="password" name="password" />
  <br>
   
  请确认您密码<br><input type="text" id="pwd" name="pwd"/>
  <br> 
  <input type="submit" id="button" name="button" onclick="kkc()" value="点击注册"/>
  <input type="reset" id="chongzhi" name="chongzhi" value="信息重置"/>
   












public class t1 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String name = req.getParameter("name") ;
String password = req.getParameter("password") ;
  String pwd = req.getParameter("pwd") ;
  if(password.equals(pwd))
  {
  req.getSession().setAttribute("name", name);
  req.getSession().setAttribute("password", password);
  resp.sendRedirect("welcome.jsp");
  }else{
  resp.sendRedirect("shibai.jsp");
  }
   
}
}


请问我测试了下,值为空也能通过验证 应该在JAVA的if(password.equals(pwd))判断账户密码非空好 还是在前台 if(""==password){
alert("亲,密码不能为空哦") 直接判断跳出好,该怎么判断呢?
 

------解决方案--------------------
先把所有的alert后面加上return false;
把kkc方法提到form里。写form 的onSubmit事件,onSubmit=return kkc()
这样就可以了
------解决方案--------------------
当然还是用JS判断比较好,前后台交互太多还是不太好的。。
或者说你给一个隐藏的DIV 后台页面验证失败的时候给一个提示 这样可以防止页面跳转
<script>
function submitForm(){
mf=document.myform;
if(check(mf)){
return true;
} else {
return false;
}
}
function check(form) {
if(isNull(form.singerName.value)) {
alert("歌手名称不能为空");
return false;

if(isNull(form.songName.value)) {
aler