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

RadListBox: CheckBoxes=true and RequiredFieldValidator

21 Answers 761 Views
ListBox
This is a migrated thread and some comments may be shown as answers.
Morten
Top achievements
Rank 1
Veteran
Morten asked on 04 Aug 2009, 11:25 PM
I have a radlistbox:
<telerik:RadListBox ID="rlb" runat="server" CheckBoxes="true" />

and a reguiredfieldvalidator:
<asp:RequiredFieldValidator runat="server" ID="rfv" ControlToValidate="rlb">*</asp:RequiredFieldValidator>

The Required field validation works fine when at least one item is selected (highlighted).

How can I get the RequiredFieldValidator to accept a checkbox.checked item (not highlighted) as a valid pass?

21 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 05 Aug 2009, 10:39 AM
Hi Morten,

One option is by using CustomValidator control for validating the RadListBox as shown below. Give a try with following client side code.

ASPX:
 
 
<telerik:RadListBox runat="server" ID="RadListBox1" CheckBoxes="true" Height="200px" Width="230px"
</telerik:RadListBox> 
 
<asp:CustomValidator ID="CustomValidator1" runat="server" ClientValidationFunction="ValidationCriteria" ErrorMessage="CustomValidator"
</asp:CustomValidator> 

JavaScript:
 
<script type="text/javascript"
function ValidationCriteria(source, args) 
    var listbox = $find('<%= RadListBox1.ClientID %>'); 
    var check = 0; 
    var items = listbox.get_items(); 
    for(var i = 0; i<= items.get_count()-1; i++) 
    { 
        var item = items.getItem(i); 
        if(item.get_checked()) 
        { 
            check = 1; 
        } 
    } 
    if(check) 
        args.IsValid =true
    else 
        args.IsValid = false
</script> 

-Shinu.
0
Accepted
Veselin Vasilev
Telerik team
answered on 05 Aug 2009, 11:45 AM
Hello guys,

The javascript method could be even more optimized:

<script type="text/javascript">  
function ValidationCriteria(source, args)  
{  
    var listbox = $find('<%= RadListBox1.ClientID %>');  
    args.IsValid = listbox.get_checkedItems().length > 0;  
}  
</script>  


All the best,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
N S
Top achievements
Rank 1
answered on 02 Sep 2009, 10:15 AM
How do I validate if any checkboxes are checked once the RadListBox is used inside a RadGrid?

I'm having trouble with Validation Script: RadListBox1 is not accessible, since it is part of the EditForm of a RadGrid.

<script type="text/javascript">  
function ValidationCriteria(source, args)  
{  
    var listbox = $find('<%= RadListBox1.ClientID %>');  //RadListBox1 is part of the EditForm of a grid, so it cannot be accessed here 
    args.IsValid = listbox.get_checkedItems().length > 0;  
}  
</script> 
);  


I cannot use the findControl() function of the RadGrid to find the RadListBox1. How do I find the RadListBox1 to validate it? Or is there another way?
0
Accepted
Veselin Vasilev
Telerik team
answered on 02 Sep 2009, 02:36 PM
Hello,

Please check this help topic on how to easily find a reference to the RadListBox object by using a global javascript variable and the OnClientLoad event.

All the best,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
N S
Top achievements
Rank 1
answered on 02 Sep 2009, 03:19 PM
This works great, thank you.

Kind regards
0
Regino Rivera
Top achievements
Rank 1
answered on 07 Oct 2009, 04:39 PM
Hi N S:

I'm trying to do exactly what you did but I still get errors when trying to validate the RadListBox that is inside the RadGrid ... could you please put all together your code? Thanks a lot
0
Bill
Top achievements
Rank 2
answered on 08 Dec 2009, 12:09 AM
No errors on the page, but it doesn't do anything either...




   <script language="javascript" type="text/javascript">  
        function validatelistbox1(source, args) {  
            var listbox = $find('<%= rlbEvalReason.ClientID  %>');  
            args.IsValid = (listbox.get_checkedItems().length > 0);  
        }  
        function validatelistbox2(source, args) {  
            var listbox = $find('<%= rlbErgoExp.ClientID  %>');  
            args.IsValid = (listbox.get_checkedItems().length > 0);  
        }  
 
    </script> 
   
 
                             <td valign="middle">  
                                    <style="font-family: arial" >Evaluation</a><br /> 
                                    <style="font-family: arial">Reason:</a> 
                                </td> 
                                <td> 
                                    <asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="rlbEvalReason" 
                                        ErrorMessage="You Must Choose At Least 1" ClientValidationFunction="validateListbox1">   
                                    </asp:CustomValidator> 
                                    <telerik:RadListBox ID="rlbEvalReason" runat="server" DataKeyField="REASON_ID"   
                                        DataSortField="REASON_DESC" DataSourceID="sdsEvalReason"   
                                        DataTextField="REASON_DESC" DataValueField="REASON_ID" CheckBoxes="true">  
                                    </telerik:RadListBox> 
                                </td> 
                                <td>&nbsp;&nbsp;</td> 
                                <td valign="middle">  
                                    <style="font-family: arial" >Ergo</a><br /> 
                                    <style="font-family: arial" >Exposure:</a> 
                                </td> 
                                <td> 
                                    <asp:CustomValidator ID="CustomValidator2" runat="server"  ControlToValidate="rlbErgoExp" 
                                        ErrorMessage="You Must Choose At Lease 1" ClientValidationFunction="validateListbox2">  
                                    </asp:CustomValidator> 
                                    <telerik:RadListBox ID="rlbErgoExp" runat="server" DataKeyField="DESCRIPTION"   
                                        DataSortField="DESCRIPTION" DataSourceID="sdsErgoExposures"   
                                        DataTextField="DESCRIPTION" DataValueField="DESCRIPTION" CheckBoxes="True">  
                                    </telerik:RadListBox> 
                                  
                                </td> 
   
0
Veselin Vasilev
Telerik team
answered on 11 Dec 2009, 10:00 AM
Hi Bill,

I noticed two problems with your code:

1. The name of the javascript function was not the same case, compare
ClientValidationFunction="validateListbox1" with validatelistbox1
The casing should be the same

2. Please remove the ControlToValidate property of the custom validators.

Hope this helps

Greetings,
Veselin Vasilev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Bill
Top achievements
Rank 2
answered on 11 Dec 2009, 08:21 PM
Thanks,

I really wish MS would decide whether all of their dev. languages are case sensitive or not - or that I could stop having to jump back and forth between them....
0
Hendra
Top achievements
Rank 1
answered on 20 Mar 2010, 12:54 AM
Why do we need to "remove the ControlToValidate property of the custom validators" as specified below?
If we remove it, the listbox will only be validated after the page is post back.

Please advise.
Thank you,
Hendra
0
Veselin Vasilev
Telerik team
answered on 24 Mar 2010, 09:57 AM
Hi Hendra,

The reason is because when you have a RadListBox with ID = "myListBox" actually that ID becomes the ID of the outer DIV that surrounds the listbox items. Setting the ControlToValidate="myListBox" will try to validate the DIV with that ID which is not what you want.

Sincerely yours,
Veskoni
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Jason
Top achievements
Rank 1
answered on 17 Aug 2010, 08:43 AM
To perform Client Validation on checkbox checking you can manually trigger validation using the OnClientItemChecked event handler

<telerik:RadListBox runat="server" ID="chkListTrainers" CheckBoxes="true" OnClientSelectedIndexChanging="OnClientSelectedIndexChangingHandler" EmptyMessage="There are no trainers to choose from" Height="150px" Width="300px" OnClientItemChecked="OnClientItemChecked" EnableEmbeddedSkins="false" />
<br />
<asp:CustomValidator runat="server" ID="RFVTrainer" CssClass="warning" ClientValidationFunction="ValidateTrainers" ErrorMessage="* At least one trainer required" ValidationGroup="Access"/>


function OnClientSelectedIndexChangingHandler(sender, e) {
    e.get_item().set_checked(!e.get_item().get_checked());
    if (typeof(Page_ClientValidate) == 'function')
    {
        Page_ClientValidate('Access');
    }
    e.set_cancel(true);
}
         
function ValidateTrainers(source, args)
{
    var listbox = $find('<%= chkListTrainers.ClientID %>');
    args.IsValid = listbox.get_checkedItems().length > 0;
}
 
function OnClientItemChecked(sender, e)
{
    if (typeof(Page_ClientValidate) == 'function')
    {
        Page_ClientValidate('Access');
    }
}
0
mukesh
Top achievements
Rank 1
answered on 25 Oct 2010, 10:48 AM
hi
this is work afer click on listbox ,butt in my case i want if user click save without selecting list item validation should fire. i tries to call java script on client click of bottun but it doesn't work. please let me known that how it is poassible.
 thanks
 mukesh

0
Jason
Top achievements
Rank 1
answered on 29 Oct 2010, 04:03 AM
Are you using an ASP button or a html button?

If it's an ASP Button then make sure you have set CausesValidation="true".
0
mukesh
Top achievements
Rank 1
answered on 30 Oct 2010, 04:36 PM
i have created  news Regarding our Website...they are lot many..many many..
my  question is...can we add some search control for these news,so that user can search news and can see the description...

or we can made a search control on to this information ...if then please let me know...in which datatable it is being stored.
0
mukesh
Top achievements
Rank 1
answered on 01 Nov 2010, 11:46 AM
Hi, we are creating a newspage  for our website.,  this page is  containing  a huge information.,,,we want  to enable a web user to search those news.,is there any kind of feature provided in sitefinity or not.
0
Jason
Top achievements
Rank 1
answered on 09 Nov 2010, 12:00 AM
Hi Mukesh,

I suggest you start a new discussion, possibly under the "General Discussion" section, for this query.

Cheers,
Lourein
0
Thad
Top achievements
Rank 2
answered on 17 Feb 2011, 06:15 PM
Hi Veskoni ,

If we can not set the ControlToValidate, how do we get the RadListBox to work with validation extenders that use the control to validate element to position a tooltip over the appropriate control that is in error?

Thanks!
Thad
0
Dimitar Terziev
Telerik team
answered on 22 Feb 2011, 08:25 AM
Hi Thad,

I'd like to ask you to open a support ticket with your current problem, since this forum thread has already been filled with off-topic posts.
Thank you and sorry for the inconvenience caused.

Kind regards,
Dimitar Terziev
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Bill
Top achievements
Rank 1
answered on 02 Mar 2011, 01:54 AM
In my case. I do not care if items are checked, I simply want to ensure the RadListBox has items in it, that were transferred from another RadListbox to the left of it.


This works:

<telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
<script language="javascript" type="text/javascript">  
    function validateListBox(sender, args) {
        var listbox = $find('<%= RadListBox2.ClientID %>');
        var check = 0;
        var items = listbox.get_items();
        var cnt = items.get_count();
        if (cnt)
            args.IsValid = true;
        else
            args.IsValid = false;  
    }
</script>
</telerik:RadCodeBlock>

0
Tash
Top achievements
Rank 1
answered on 18 May 2012, 09:53 PM
Is it possible to make 2 checkboxes required on a radlistbox unless antoher checkbox is checked that is not part of the radlistbox then only one checking inside the radlistbox is required. If so can you provide an example.
Tags
ListBox
Asked by
Morten
Top achievements
Rank 1
Veteran
Answers by
Shinu
Top achievements
Rank 1
Veselin Vasilev
Telerik team
N S
Top achievements
Rank 1
Regino Rivera
Top achievements
Rank 1
Bill
Top achievements
Rank 2
Hendra
Top achievements
Rank 1
Jason
Top achievements
Rank 1
mukesh
Top achievements
Rank 1
Thad
Top achievements
Rank 2
Dimitar Terziev
Telerik team
Bill
Top achievements
Rank 1
Tash
Top achievements
Rank 1
Share this question
or