RadCombobox with checkboxes client side validation

3 posts, 1 answers
  1. Kothai
    Kothai avatar
    9 posts
    Member since:
    Jul 2013

    Posted 28 Aug 2013 Link to this post

    Hello,
    I have a RADCombobox which holds list of years with checkboxes for multiple year selection.
    the year starts with 2011 and and it increments by 1 till the current year. Means ComboBox will hold the values like 2011,2012,2013.
    Here I need to validate the selection at client side. Requirement is, user can select a single value or two values. If the selection is more than one then the selection should be consecutive. 
    Eg: user can select 2011 & 2012 but not 2011 & 2013, the difference should be one.

    Please help me with java script for this.
  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 28 Aug 2013 Link to this post

    Hi Kothai,

    Please check the following code I tried which works fine at my end.

    ASPX:
    <telerik:RadComboBox ID="RadComboBox1" runat="server" CheckBoxes="true" OnClientLoad="OnClientLoad"
        OnClientItemChecked="OnClientItemChecked">
    </telerik:RadComboBox>

    JavaScript:
    <script type="text/javascript">
        function OnClientLoad(sender, args) {
            var comboItem;
            var year = new Date().getFullYear();
            sender.trackChanges();
            for (var i = 2011; i <= year; i++) {
                comboItem = new Telerik.Web.UI.RadComboBoxItem();
                comboItem.set_text(i.toString());
                sender.get_items().add(comboItem);
            }
            sender.commitChanges();
        }
        function OnClientItemChecked(sender, args) {
            if (sender.get_checkedItems().length > 1) {
                var item1 = sender.get_checkedItems()[0]._text;
                var item2 = sender.get_checkedItems()[1]._text;
                var item3 = item2 - item1;
                if (item3 != 1) {
                    args.get_item().uncheck();
                    alert("Check two consecutive years");
                }
                if (sender.get_checkedItems().length > 2) {
                    args.get_item().uncheck();
                    alert("You can check only two");
                }
            }
        }
    </script>

    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kothai
    Kothai avatar
    9 posts
    Member since:
    Jul 2013

    Posted 28 Aug 2013 Link to this post

    Hello Shinu,
    Yes it works. Thank You.
Back to Top