为什么不能检测username的长度?

szsailing 2002-04-23 04:46:02
代码如下:
原意如下,让javavscript函数去验证表单输入的正确性,

if ((theform.username.value.length > 16) && (theform.username.value.length < 3))
{
alert("字符越界。");
return false;
}
这一段总是不能执行!
那位大侠不吝赐教
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> aaa</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<LINK rel="stylesheet" type="text/css" href="xxxxxxxxxx.css">

</HEAD>

<BODY><script language="javascript">
function validate(theform) {
if (theform.username.value=="" && theform.password.value=="")
{
alert("用户名和密码项不能为空!");
return false;
}
if (theform.username.value=="")
{
alert("请输入用户名!");
return false;
}
if (theform.password.value=="")
{
alert("请输入密码!");
return false;
}
if ((theform.username.value.length > 16) && (theform.username.value.length < 3))
{
alert("字符越界。");
return false;
}
//判断用户名的长度和密码的长度。
}
</script>
<form method="POST" name="vbform" onSubmit="return validate(this)" action="admin/login_form_output.php">
<table border="1" width="64%">
<tr>
<td width="42%">
<p align="right">用户名</p>
</td>
<td width="58%"><input type="text" name="username" size="20"></td>
</tr>
<tr>
<td width="42%">
<p align="right">密码</p>
</td>
<td width="58%"><input type="password" name="password" size="20"></td>
</tr>
</table>
<p><input type="submit" value="提交" name="submit"><input type="reset" value="重置" name="reset"></p>
</form>

</body>
</html>
...全文
52 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiushuiwuhen 2002-04-23
  • 打赏
  • 举报
回复
if (!(theform.username.value.length < 16 && theform.username.value.length > 3))
孟子E章 2002-04-23
  • 打赏
  • 举报
回复
&&:表示And
||:表示 Or
孟子E章 2002-04-23
  • 打赏
  • 举报
回复
if ((theform.username.value.length > 16) && (theform.username.value.length < 3))
{
alert("字符越界。");
return false;
}

当然不能同时满足了:

if ((theform.username.value.length > 16) || (theform.username.value.length < 3))
{
alert("字符越界。");
return false;
}

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧