This is a migrated thread and some comments may be shown as answers.
RadCombobox with checkboxes client side validation
2 Answers 53 Views
This is a migrated thread and some comments may be shown as answers.
Kothai
Top achievements
Rank 1
Kothai asked on 28 Aug 2013, 09:49 AM
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 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 1
answered on 28 Aug 2013, 12:15 PM
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.
0
Kothai
Top achievements
Rank 1
answered on 28 Aug 2013, 01:22 PM
Hello Shinu,
Yes it works. Thank You.
Asked by
Kothai
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Kothai
Top achievements
Rank 1
Share this question
or