首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

小白求指教解决方案

2012-03-21 
小白求指教script typetext/javascriptfunction kkc(){var biaodandocument.getElementById(biaoda

小白求指教
<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)) {
alert("歌曲名称不能为空");
return false;
}
if(isNull(form.types.value)) {
alert("歌曲类别不能为空");
return false;


}

if (!/\.(mp3|WMA)$/.test(document.getElementById("upload").value)) {
alert("文件类型必须是.MP3.WMA中的一种");
document.getElementById("upload").value = "";
return false;

return true;

}


</script>

<script type="text/javascript"> 
function isNull(str) {
if(str==null||str.length==0) {
return true;
}else {
return false;
}
}
  

</script>


给你一个例子 你看看
你可以调试一下alert一下看看password 不添加内容是的时候获取的是什么值你就明白了
[解决办法]
前台后台都判断,前台判断可以减少用户的错误提交,后台判断,其实没有什么大作用,但是用参数的时候想判断是否为空是编程的好习惯
[解决办法]
确实是需要前后台都判断的、

js是客户端执行的脚步语音、

而客户端的人若是知道存放位置就可以跳过你的js判断来插入和修改非法信息

后台在拦截一次的话可以尽可能少的让他们插入修改非法信息。

热点排行