This is a migrated thread and some comments may be shown as answers.

validate listbox items client side code

4 Answers 179 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
arturo
Top achievements
Rank 1
arturo asked on 18 May 2011, 05:30 PM

hi all

i have a little problem with radlistbox, already i add items from a radcombobox, this function run correctly, but radlistbox accept repeated items, i want to know how i can validate with client side code if items on list do repeated before to add new item

thanks in advance for read this thread and for all answers

Regards!

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 19 May 2011, 11:07 AM
Hello Arturo,

RadListBox provides a flexible client-side API. You can easily interact with the listbox in the browser using its client-side object.
You get the total items by get_items() method and loop through it and check it with the newly added items.

Check out the following help article for more on client side API.
RadListBox client API

Thanks,
Shinu.
0
arturo
Top achievements
Rank 1
answered on 19 May 2011, 03:38 PM

hi shinu, thanks for reply my question, but i have a problem with the code i've wrote, the insert of new item proceed correctly, but the loop where i count the items collection of radlistbox not execute correctly, the IE not throw me an error of client side code, i guess i'm doing something wrong

this is my code


<script type="text/javascript">
         
           function AddNewItem() {
           var lb = $find("<%= lstusers.ClientID %>"); // search radlistbox
           var itemtext = $find("<%= cmbusers.ClientID %>").get_value(); //selected item from a combobox
           if (!itemtext) {
                     alert("Please select a user to add in the group.");
                     return false;
                 }                        
//this is the loop that use to count the radlistbox items and validate they items not repeat
           for (i=0;i<=lb.get_items().count;i++){
                if (lb.getItem[i].get_value() = itemtext){
                    alert("this user already exists in the list");
                    return false;
                }
           }
             
           var item = new Telerik.Web.UI.RadListBoxItem();
           item.set_text(itemtext);
           lb.trackChanges();
           lb.get_items().add(item);
           item.select();
           lb.commitChanges();
       }
0
Accepted
Shinu
Top achievements
Rank 2
answered on 20 May 2011, 10:49 AM
Hello Arturo,

I have made some changes in your code.

Javascript:
<script type="text/javascript">
    function AddNewItem()
 {
        var lb = $find("<%= lstusers.ClientID %>");
        var itemtext = $find("<%= cmbusers.ClientID %>").get_value();
        if (!itemtext)
        {
            alert("Please select a user to add in the group.");
            return false;
        }
        for (i = 0; i < lb.get_items().get_count(); i++) {
            if (lb.getItem(0).get_value() == itemtext) {
                alert("this user already exists in the list");
                return false;
            }
        }
        var item = new Telerik.Web.UI.RadListBoxItem();
        item.set_text(itemtext);
        lb.trackChanges();
        lb.get_items().add(item);
        item.select();
        lb.commitChanges();
    }
</script>

Also take a look at the following help article for more on on RadListBox client side API.
RadListBoxItem client API.

Thanks,
Shinu.
0
arturo
Top achievements
Rank 1
answered on 20 May 2011, 04:00 PM
thx shinu!!

its work perfectly!!
Tags
ListBox
Asked by
arturo
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
arturo
Top achievements
Rank 1
Share this question
or