列表框选择值验证. [英] Listbox selected value validation.

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

问题描述

我有一个列表框.现在,我要验证列表框以选择最大三个值.如果选择的值大于三个,则应发出消息.请帮助我执行此操作.

I have a list box. Now I want to validate listbox to selecting maximum three value. If selected value more then three it should give message. Please help me to do this.

推荐答案

您可以通过JavaScript进行此操作.这是一个示例
You can do this through JavaScript. Here is a sample
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return ValidateListBox()" />
 <br />
    <asp:ListBox ID="Listbox1" runat="server" SelectionMode="Multiple">
    <asp:ListItem Text="Item 1" Value="Item 1"></asp:ListItem>
    <asp:ListItem Text="Item 2" Value="Item 2"></asp:ListItem>
    <asp:ListItem Text="Item 3" Value="Item 3"></asp:ListItem>
    <asp:ListItem Text="Item 4" Value="Item 4"></asp:ListItem>
    <asp:ListItem Text="Item 5" Value="Item 5"></asp:ListItem>
    <asp:ListItem Text="Item 6" Value="Item 6"></asp:ListItem>
    <asp:ListItem Text="Item 7" Value="Item 7"></asp:ListItem>
    </asp:ListBox>



这是单击Button1时调用的JavaScript函数.它检查在列表框中选择的项目数.如果所选项目的数量超过3,则会显示一条警告消息.



Here is the JavaScript function that gets called when Button1 is clicked. It checks for the number of items selected in the listbox. If the count of selected items exceeds 3 it shows an alert message.

<script type="text/javascript">
    function ValidateListBox()
    {

        var ListBox = document.getElementById('<%=Listbox1.ClientID %>');
        var length = ListBox.length;
        var i = 0;
        var SelectedItemCount = 0;

        for(i=0;i<length;i++)

        {

            if(ListBox.options[i].selected)

            {

                SelectedItemCount = SelectedItemCount + 1;

            }



            if(SelectedItemCount > 3)
            {
                alert('More than 3 items selected in listbox.');
                return false;
            }
        }


    }


    </script>



希望这会有所帮助.



Hope this helps.


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

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