RadListBox: CheckBoxes=true and RequiredFieldValidator

22 posts, 2 answers
  1. Morten
    Morten avatar
    296 posts
    Member since:
    Jul 2012

    Posted 04 Aug 2009 Link to this post

    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?
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 05 Aug 2009 Link to this post

    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.
  3. Answer
    Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 05 Aug 2009 Link to this post

    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.
  4. N S
    N S avatar
    3 posts
    Member since:
    Sep 2009

    Posted 02 Sep 2009 Link to this post

    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?
  5. Answer
    Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 02 Sep 2009 Link to this post

    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.
  6. N S
    N S avatar
    3 posts
    Member since:
    Sep 2009

    Posted 02 Sep 2009 Link to this post

    This works great, thank you.

    Kind regards
  7. Regino Rivera
    Regino Rivera avatar
    1 posts
    Member since:
    Dec 2008

    Posted 07 Oct 2009 Link to this post

    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
  8. Bill
    Bill avatar
    85 posts
    Member since:
    Oct 2007

    Posted 07 Dec 2009 Link to this post

    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> 
       
  9. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 11 Dec 2009 Link to this post

    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.
  10. Bill
    Bill avatar
    85 posts
    Member since:
    Oct 2007

    Posted 11 Dec 2009 Link to this post

    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....
  11. Hendra
    Hendra avatar
    2 posts
    Member since:
    Jul 2012

    Posted 19 Mar 2010 Link to this post

    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
  12. Veselin Vasilev
    Admin
    Veselin Vasilev avatar
    2992 posts

    Posted 24 Mar 2010 Link to this post

    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.
  13. Jason
    Jason avatar
    26 posts
    Member since:
    Nov 2006

    Posted 17 Aug 2010 Link to this post

    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');
        }
    }
  14. mukesh
    mukesh avatar
    5 posts
    Member since:
    Aug 2010

    Posted 25 Oct 2010 Link to this post

    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

  15. Jason
    Jason avatar
    26 posts
    Member since:
    Nov 2006

    Posted 28 Oct 2010 Link to this post

    Are you using an ASP button or a html button?

    If it's an ASP Button then make sure you have set CausesValidation="true".
  16. mukesh
    mukesh avatar
    5 posts
    Member since:
    Aug 2010

    Posted 30 Oct 2010 Link to this post

    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.
  17. mukesh
    mukesh avatar
    5 posts
    Member since:
    Aug 2010

    Posted 01 Nov 2010 Link to this post

    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.
  18. Jason
    Jason avatar
    26 posts
    Member since:
    Nov 2006

    Posted 08 Nov 2010 Link to this post

    Hi Mukesh,

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

    Cheers,
    Lourein
  19. Thad
    Thad avatar
    106 posts
    Member since:
    Sep 2010

    Posted 17 Feb 2011 Link to this post

    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
  20. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 22 Feb 2011 Link to this post

    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!
  21. Bill
    Bill avatar
    42 posts
    Member since:
    Feb 2011

    Posted 01 Mar 2011 Link to this post

    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>

  22. Tash
    Tash avatar
    15 posts
    Member since:
    Jul 2012

    Posted 18 May 2012 Link to this post

    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.
Back to Top