如何检验

lance09 2003-04-25 01:58:36
我想在浏览器端,验证用户没有进行选择,在用户提交表单时,当有选择时将数据传到服务器处理,没有选择则提

示用户进行选择.于是我写了如下的程序,但是当只有一个checkbox时,就不对了,请高手指点!


<HTML>
<HEAD><TITLE>简单验证</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Submit1_OnClick
Dim TheForm
Set TheForm = Document.ValidForm
dim i,sel
sel=false
'msgbox theform.c1.length
for i=0 to theform.c1.length-1
if theform.c1(i).checked then
sel=true
exit for
end if
next
If sel Then
TheForm.Submit() ' 数据输入正确,传递到服务器。
else
msgbox "请进行选择"
End If
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H3>简单验证</H3><HR>
<FORM NAME="ValidForm">
<input type=checkbox name=c1 value="2">2<br>
<input type=checkbox name=c1 value="3">3<br><!--把这两行注释就不对了-->
<input type=checkbox name=c1 value="1">1<br><!--把这两行注释就不对了-->
<INPUT NAME="Submit1" TYPE="BUTTON" VALUE="提交">
<input type="button" name="ch2" VALUE="jump"

onclick="window.location.href='http://www.google.com'">
</FORM>
</BODY>
</HTML>
...全文
143 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tigerwen01 2003-04-25
  • 打赏
  • 举报
回复
<HTML>
<HEAD><TITLE>简单验证</TITLE>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Submit1_OnClick
Dim TheForm
Set TheForm = Document.ValidForm
dim i,sel
sel=false
if theform.c1.checked then
sel=true

end if

If sel Then
TheForm.Submit() ' 数据输入正确,传递到服务器。
else
msgbox "请进行选择"
End If
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<H3>简单验证</H3><HR>
<FORM NAME="ValidForm">
<input type=checkbox name=c1 value="2">2<br/>
<INPUT NAME="Submit1" TYPE="BUTTON" VALUE="提交">
<input type="button" name="ch2" VALUE="jump"

onclick="window.location.href='http://www.google.com'">
</FORM>
</BODY>
</HTML>
-----------------------------------------
说明,只有一项用数组来处理当然不对!以上代码修改后是正确的。
iStringTheory 2003-04-25
  • 打赏
  • 举报
回复
更保险一点的是:
if theform.c1.length=1 then
if theform.c1.checked then
sel=true
exit for
end if
elseif theform.c1.length>1 then
for i=0 to theform.c1.length-1
if theform.c1(i).checked then
sel=true
exit for
end if
next
end if
wang11912 2003-04-25
  • 打赏
  • 举报
回复
for each radioval in thform.c1
if c1.checked then
sel=true
exit for
end if
next
Reker熊 2003-04-25
  • 打赏
  • 举报
回复
<SCRIPT LANGUAGE="JavaScript">
function check()
{
var a=document.ValidForm;
for(i=0;i<a.elements.length;i++)
{
if((a.elements[i].type=="checkbox")&&(a.elements[i].checked==true))
{a.submit();return;}
}
alert("Must Check One");
return(false);
}
</SCRIPT>
</HEAD>
<BODY>
<H3>简单验证</H3><HR>
<FORM NAME="ValidForm">
<input type=checkbox name=c1 value="2">2<br>


<INPUT NAME="Submit1" TYPE="BUTTON" VALUE="提交" onclick="return(check())">
<input type="button" name="ch2" VALUE="jump"

onclick="window.location.href='http://www.google.com'">
</FORM>
iStringTheory 2003-04-25
  • 打赏
  • 举报
回复
原因是,1个元素构不成数组,当然不能用数组的方法来访问,解决方法是分开处理:


Sub Submit1_OnClick
Dim TheForm
Set TheForm = Document.ValidForm
dim i,sel
sel=false
'msgbox theform.c1.length
select case theform.c1.length
case 1
if theform.c1.checked then
sel=true
exit for
end if
case else
for i=0 to theform.c1.length-1
if theform.c1(i).checked then
sel=true
exit for
end if
next
end select
If sel Then
TheForm.Submit() ' 数据输入正确,传递到服务器。
else
msgbox "请进行选择"
End If
End Sub

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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