首 页   · 站长博客 · 用户注册 · 会员登陆  · 会员排行  ·最新主题  ·最近回复  精华区  版权声明  ·论坛管理
  当前登录身份:游客,请先登录。  笔名: 口令: 验证码:   
楼 主  index »  PHP+MYSQL编程 » [原创]客户端表单验证  


  作者:andymylove
  注册时间:2005-05-26
  主题/回复:8/11
  积分:451
  等级:★★☆(五级)
  称号:略有小成

  andy-my-love@163..
  27548
  www.

 

 发表:2005-06-07 23:10:34 阅读 2391 次 回复 2 次 得分2  |   字号 字色
[原创]客户端表单验证
我在做注册表单时想用javascript在客户端验证是否为空后在去进一步让服务器验证,我写的部分代码如下,可是老是只能验证用户名这一项,也

就是说我填了用户名后如果其他项不填的话,就不回在弹出对话框警告你了.
就是下面这段代码如果去掉第3个if前面的注释符后就可以得到验证,但只能验证用户名这一项.
我现在想让javascript在客户端依次将这些项都验证后再去服务器验证,请问斑竹这应该怎么该呀??
如果我没说清楚的话,还麻烦斑竹亲自运行以下这段代码看看问题究竟出在哪了,好吗?
            
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>zhucebiaodan</title>
<script language=JavaScript>
 function checkSubmit()
  {  if((document.form1.username.value) =="")
   { window.alert("请输入用户名");
 documet.form1.username.focus();
 return false;
   }
 else 
   {return true; 
   }
 if((documet.form1.password.value) =="")
   {  window.alert("请输入正确密码");
  document.form1.password.focus();
  return false;
   }
  else
   {return true;
   }
   /* if((document.form1.password.length)<4"")
   { window.alert("请输入正确密码");
 document.form1.password.focus();
  return false;
 }
  else
   {return true;
   }
  if((document.form1.password.value)!=(document.form1.repassword.value) )
   { window.alert("您两次输入的密码不同,请重输");
     document.form1.repassword.focus();
     return false;  
   } 
  else
   {return true;
   }
  if((document.form1.nick.value)=="")
    {  window.alert("请输入昵称");
   document.form1.nick.focus();
  return false;
 }
   else
   {return true;
   } */
   return true;
 }

</script>
</head>

<body>
<form  name="form1" action="register.php" method="post" onsubmit="javascript:return checkSubmit()">
   <table width="789" height="442" border="0" align="center" cellpadding="3" cellspacing="0" bgcolor="#66CC66">
    <tr> 
  <td width="83"> 用户ID:</td>
  <td width="694"> <input type="text" name="textfield"> </td>
    </tr>
    <tr> 
  <td>用户名:</td>
  <td> <input type="text" name="username"> </td>
    </tr>
    <tr> 
  <td>密码:</td>
  <td><input type="password" name="password">
     <?php if($erro=="1") echo "用户名已存在"  ?></td>
    </tr>
    <tr>
  <td height="31"> </td>
  <td><p><font face="隶书"><strong>合法密码必须由4到20各字符组成,</strong></font><font face="隶书"><strong>可由英文字母

(大小写不限)和阿拉伯数字组成;</strong></font></p>
    </td>
    </tr>
    <tr> 
  <td>重复密码:</td>
  <td><input type="password" name="repassword"></td>
    </tr>
    <tr> 
  <td>昵称:</td>
  <td><input type="text" name="nick"></td>
    </tr>
    <tr> 
  <td>姓名:</td>
  <td><input type="radio" name="xingming" value="radiobutton">
    男
    <input type="radio" name="" value="radiobutton">
    女</td>
    </tr>
    <tr> 
  <td>性别:</td>
  <td><input type="text" name="textfield6"></td>
    </tr>
    <tr> 
  <td>年龄:</td>
  <td><input type="text" name="textfield7"></td>
    </tr>
    <tr> 
  <td>地区:</td>
  <td><select name="select">
  <option selected>北京</option>
  <option>上海</option>
  <option>西安</option>
  <option>深圳</option>
  <option>大连</option>
    </select></td>
    </tr>
    <tr> 
  <td height="71">爱好:</td>
  <td><p> 
  <input type="checkbox" name="checkbox" value="checkbox">
  音乐 
  <input type="checkbox" name="checkbox2" value="checkbox">
  体育 
  <input type="checkbox" name="checkbox3" value="checkbox">
  看书 
  <input type="checkbox" name="checkbox4" value="checkbox">
  文学</p>
    <p> 
  <input type="checkbox" name="checkbox5" value="checkbox">
  交友 
  <input type="checkbox" name="checkbox6" value="checkbox">
  聊天 
  <input type="checkbox" name="checkbox7" value="checkbox">
  军事 </p>
    <p> 
  <input type="checkbox" name="checkbox8" value="checkbox">
  上网 
  <input type="checkbox" name="checkbox9" value="checkbox">
  游戏 </p></td>
    </tr>
    <tr> 
  <td>详细地址:</td>
  <td><input type="text" name="textfield112"></td>
    </tr>
    <tr> 
  <td>QQ:</td>
  <td><input type="text" name="textfield11"></td>
    </tr>
    <tr> 
  <td>Email:</td>
  <td><input type="text" name="textfield12"></td>
    </tr>
    <tr> 
  <td>Tel:</td>
  <td><input type="text" name="textfield13"></td>
    </tr>
    <tr> 
  <td colspan="2"><input type="submit" name="Submit" value="提交">   <input type="submit" name="Submit2" value="重置

"></td>
    </tr>
  </table>
</form>
</body>
</html>
 
 1#楼  
 
  回复人:pnrj7999
  注册时间:2005-04-04
  主题/回复:73/43
  积分:813
  等级:★★★(六级)
  称号:声名鹊起

用户联系方式已设置为保密
 1#楼 发表于2005-06-08 16:21:47  评分:1 

你在第一个查用户名时,用户名正确就返回true了,当然不往下检查了,把return true全去掉,最后检查都没错了,才return.
 2#楼  
 
  回复人:一起PHP
  注册时间:2004-02-27
  主题/回复:247/1521
  积分:4649
  等级:★★★★★☆(十一级)
  称号:论坛圣人

   
 2#楼 发表于2005-06-08 16:22:13  评分:1 

把所有的
else 
   {return true; 
   }
删掉即可。

遇到一个 return 。那么return后面的语句就全部失效了。
只需要判断如果为空的话,就return false就可以了。
不用写 else{ return true}
比如:

 function checkSubmit(){
  if(....==""){
   alert(...);
   return false;
  }
  if(...==""){
   alert(...);
   return false;
  }
  ..............
}
  页数1/1首页 « 1 » 末页
  发表回复:您还没有登陆,无法发表回复。请先[登陆]

一起PHP技术联盟 主办:一起PHP 联系方式:站长QQ:4304410 QQ群:8423742 20159565 站长博客 E-mail: nqp@nqp.me 执行时间:0.005sec
SimsmaBBS 2008 (v6.0) Developed by 17php.com,Copyright(C)2003-2010 All rights reserved. 副本授权:一起PHP官方专用版