验证多个复选框 [英] validation for multiple checkboxes

查看:120
本文介绍了验证多个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组复选框.
我需要进行验证,以便必须选中至少一个复选框,否则我应该得到一条消息,以选择至少一个复选框.

I have a group of checkboxes.
I need a validation such that atleast one checkbox must be checked or else I should get a message to select at least one checkbox.

Can any one help me out?

推荐答案

您好
您可以使用Java脚本进行验证.这是三个复选框的示例.

Hi
You can use java script to validate it. Here is an example for three check boxes .

<script type="text/javascript" language="JavaScript">
<!--
function checkCheckBoxes(theForm) {
    if (
    theForm.CHECKBOX_1.checked == false &&
    theForm.CHECKBOX_2.checked == false &&
    theForm.CHECKBOX_3.checked == false)
    {
        alert ('Select any check box!');
        return false;
    } else {
        return true;
    }
}
//-->
</script>




如果有更多复选框,请使用"for"循环进行迭代以查找是否选中了任何一个.为此,复选框名称必须类似于示例中的索引索引

如果您的需求不满意,请阅读




if you have more check boxes , use ''for'' loop to iterate to find whether any one is checked or not. For this the check box names must be like a indexed one as in example

if your need is not satisfied please read this


使用

You can use

var elements = document.getElementsByName(''groupname'');

for(var i=0; i< elements.length; i++)
 //check if any elements[i] is checked




:cool:




:cool:


简单起见,
使用RequiredFieldValidatorCheckBoxList
如果未选中任何复选框,它将抛出味精.
For a Simple,
Use RequiredFieldValidator to CheckBoxList
it will throw msg if No checkbox is selected.


这篇关于验证多个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆