Max number of tokens

10 posts, 0 answers
  1. Angie
    Angie avatar
    101 posts
    Member since:
    Jan 2008

    Posted 23 Apr 2013 Link to this post

    Is it possible to limit the number of tokens inputted into an autocomplete box via some sort of property or setting?

    Thanks!
  2. MasterChiefMasterChef
    MasterChiefMasterChef avatar
    152 posts
    Member since:
    Jan 2013

    Posted 23 Apr 2013 Link to this post

    Hi Angie,

    The 'maxlength' property should do what you are looking for. It works with both HTML5 input types and the RadComboBox.

    <input id="Text1" type="text" maxlength="10"/>
    <telerik:RadComboBox    maxlength="5" runat="server" AllowCustomText="True"></telerik:RadComboBox>
        

    Hopefully this helps,
    Master Chief
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Angie
    Angie avatar
    101 posts
    Member since:
    Jan 2008

    Posted 23 Apr 2013 Link to this post

    Hi.  I think you're talking about something different. 

    I'm working with the RadAutoComplete box, using the Token input type.  http://demos.telerik.com/aspnet-ajax/autocompletebox/examples/default/defaultcs.aspx

    I'm trying to limit the number of tokens a user can enter into the AutoComplete box.  The MaxLength property is not available in this control.

    Thanks anyway, though. :)
  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Apr 2013 Link to this post

    Hi,

    You can attach the OnClientDropDownOpening client event to the RadAutoCompleteBox and try the following JavaScript code.

    JavaScript:
    <script type="text/javascript">
        var limit = 5;  //Setting the maximum number of entries
        function OnClientDropDownOpening(sender, args) {
            if (sender.get_entries().get_count() == limit) {
                //Checking if the entries already added is equal to count, then cancel the operation.
                args.set_cancel(true);
            }
        }
    </script>

    Thanks,
    Princy.
  6. Angie
    Angie avatar
    101 posts
    Member since:
    Jan 2008

    Posted 24 Apr 2013 Link to this post

    Hey Princy, thanks for the response.  I'm definitely headed in the right direction, but I changed the function to fire on the

    OnClientEntryAdding event. The drop down event wasn't firing fast enough, or maybe it's because I also allow custom entries.

    What I need to do now is figure out how to clear out the leftover text from the tag that won't be added.  It's not yet included in the token entry collection, so get_entries().remove does not work.  Any suggestions for clearing that last text value?

    Thanks!

  7. Kate
    Admin
    Kate avatar
    1898 posts

    Posted 29 Apr 2013 Link to this post

    Hi Angie,

    Can you provide more details on functionality that you are trying to achieve? What do you mean by the  leftover text from the tag that won't be added? A short video or an image file demonstrating the functionality would be very useful.

    Kind regards,
    Kate
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  8. Angie
    Angie avatar
    101 posts
    Member since:
    Jan 2008

    Posted 29 Apr 2013 Link to this post

    Hi Kate.  In my above response I attached an image showing what I mean by left over text.  Basically the text the someone tries to type after reaching the maximum number of tokens. 

    However, perhaps there is a much better way of handling this issue. 

    The real question is: how do I make sure a user can only enter a certain number of tokens within the radautocompletebox? 

    Thanks.

    <telerik:RadAutoCompleteBox id="radTags" Skin="Web20" Runat="server" Width="700px"  EnableViewState="true"  AllowCustomEntry="true" Delimiter="," InputType="Token"  />
  9. Bozhidar
    Admin
    Bozhidar avatar
    1101 posts

    Posted 02 May 2013 Link to this post

    Hi Angie,

    Could you try the following code patch and see if that's the behavior you're looking for:
    var originalHandler = Telerik.Web.UI.RadAutoCompleteBox.prototype._onKeyPress;
    Telerik.Web.UI.RadAutoCompleteBox.prototype._onKeyPress = function(e) {
        var code = e.keyCode ? e.keyCode : e.charCode;
     
        if (this.get_entries().get_count() >= 2 && code != Sys.UI.Key.backspace) {
            e.preventDefault();
        } else {
            originalHandler.call(this, e);
        }
    }

     

    Kind regards,
    Bozhidar
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  10. Chaim
    Chaim avatar
    18 posts
    Member since:
    Jan 2014

    Posted 04 Jul 2014 in reply to Bozhidar Link to this post

    I was trying to implement Bozhidar code, but there's a little problem, theres no "RadAutoCompleteBox" in Telerik.Web.UI.

    Is there any other way to achieve the desired behaviour?

    Thanks!
  11. Chaim
    Chaim avatar
    18 posts
    Member since:
    Jan 2014

    Posted 04 Jul 2014 in reply to Chaim Link to this post

    Figured it out... I was getting a Undefined exception, but this is because when my page loads, there's no RadAutoCompleteBox loaded, it loads until I click Edit in my RadGrid

    So I put the script inside my EditItemTemplate and now it doesn't throw an error and it works....

    regards!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017