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

NumericTextBox and IntegerTextBox - MaxLength attribute is not working

10 Answers 243 Views
NumericTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Paul
Top achievements
Rank 1
Paul asked on 28 Aug 2010, 01:13 AM

I tried the suggestion of Greg Hendee in his post here (http://www.telerik.com/community/forums/aspnet-mvc/numericinput/limit-number-of-characters-entered.aspx) but it doesn't solve my problem.  Looking at the raw html i notice there are actually 3 controls that come into play for the NumericTextBox or IntegerTextBox.  It is a div and two input tags inside the div. 

I will describe what I'm doing and what I'm observing is happening in the event that I'm doing it incorrectly:

  1. I noticed that when I add an attribute to the .HtmlAttribute() method it puts said attribute on the div described above.
  2. I also noticed that when I add an attribute to the .InputHtmlAttribute() method it sometimes puts the attribute on both input controls and sometimes doesn't.  This is where my problem is.

I am using the following code to set some attributes on my NumericTextBox and IntegerTextBox (I used both in the hopes that one would work):

<%: Html.Telerik().NumericTextBox()
        .Name("TestNumeric")
        .Value(3)
        .MinValue(1)
        .MaxValue(99)
        .DecimalDigits(0)
        .Spinners(false)
        .InputHtmlAttributes(new { maxlength = "2", style="width: 25px;" })
        .HtmlAttributes(new { maxlength = "2",style="width: 25px;" })
        .ClientEvents(events => {events.OnChange("numericTextBoxChange");})
%>
<br />
<%: Html.Telerik().IntegerTextBox()
        .Name("TestInteger")
        .Value(3)
        .MinValue(1)
        .MaxValue(99)
        .Spinners(false)
        .InputHtmlAttributes(new { maxlength = "2", style="width: 25px;" })
        .HtmlAttributes(new { maxlength = "2",style="width: 25px;" })
        .ClientEvents(events => {events.OnChange("integerTextBoxChange");})
%>

The problem is as you can see I'm specifying both the style attribute and the maxlength attribute but only the style attribute is put on both the input tags that are included in the div.  It is putting the maxlength attribute on the input control that is set to display: none and I'm not sure how to set the other input tag with this attribute.  Since it works for the style attribute, I assumed it would work for the maxlength attribute.

What am I doing wrong?

Thanks,
Paul

10 Answers, 1 is accepted

Sort by
0
Alex Gyoshev
Telerik team
answered on 30 Aug 2010, 02:22 PM
Hello Paul,

Indeed, this is a bug and we logged it for fixing. Until we do that, you can use the following work-around:
  1. remove the maxlength from the InputHtmlAttributes
  2. use the following script on the page:
        <script type="text/javascript">
            function initMaxWidth() {
                $('#TestNumeric,#TestInteger')
                    .each(function() {
                        $(this).find('.t-input:first').attr('maxlength', 2);
                    });
            }
        </script>

        <% Html.Telerik().ScriptRegistrar().OnDocumentReady(() => {%>initMaxWidth();<%}); %>

The issue will be addressed this week.

Greetings,
Alex Gyoshev
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
Paul
Top achievements
Rank 1
answered on 30 Aug 2010, 08:09 PM
Yea that would work if I wasn't dynamically loading the contents of the tab on my webpage.  Any suggestions on other places (OnDocumentReady happens before my controls are loaded on the screen) to run that javascript method?  Maybe I could do it on the "OnLoad" method of each NumericTextBox control I'm using?  If so can you give me an example of the jQuery or JavaScript I could use in the OnLoad method of the NumericTextBox controls?

Thanks,
Paul
0
Paul
Top achievements
Rank 1
answered on 30 Aug 2010, 08:28 PM
I have it working now with the OnLoad client event of the control but I'd love to be able to pass in a value to the method so I didn't have to hardcode the maxlength in the JavaScript.  I have a few controls that have different lengths and I hate to create a separate JavaScript method based on the maxlength value.  Any suggestions on how to accomplish this?

Here's the code I'm using...

JavaScript:
<script type="text/javascript">
    function initMaxWidth(e) {
        var children = $(e.currentTarget).children('.t-input');
        $(children).each(function () {
            $(this).attr('maxlength', 2);
        });
    }
</script>

Control Definition:
<%: Html.Telerik().NumericTextBox()
        .Name("TestNumeric")
        .Value(3)
        .MinValue(1)
        .MaxValue(99)
        .DecimalDigits(0)
        .Spinners(false)
        .InputHtmlAttributes(new { style="width: 25px;" })
        .HtmlAttributes(new { style="width: 25px;" })
        .ClientEvents(events => {events.OnLoad("initMaxWidth");})
%>
<br />
<%: Html.Telerik().IntegerTextBox()
        .Name("TestInteger")
        .Value(3)
        .MinValue(1)
        .MaxValue(99)
        .Spinners(false)
        .InputHtmlAttributes(new { style="width: 25px;" })
        .HtmlAttributes(new { style="width: 25px;" })
        .ClientEvents(events => {events.OnLoad("initMaxWidth");})
%>



0
Accepted
Alex Gyoshev
Telerik team
answered on 31 Aug 2010, 09:47 AM
Hello Paul,

In telerik.textbox.js, find the following code (line 34):
        $('<input>', {
            id: input.attr('id') + "-text",
            name: input.attr('name') + "-text",
            value: (inputValue || this.text),
            'class': input.attr('class'),
            style: input.attr('style')
        })

... and add the maxlength attribute like this:
        $('<input>', {
            id: input.attr('id') + "-text",
            name: input.attr('name') + "-text",
            value: (inputValue || this.text),
            'class': input.attr('class'),
            style: input.attr('style'),
            maxlength: input.attr('maxlength')
        })


This should allow you to set the maxlength through the InputHtmlAttributes. It will be included in upcoming releases.

Kind regards,
Alex Gyoshev
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
Paul
Top achievements
Rank 1
answered on 31 Aug 2010, 02:41 PM
Thanks this solved my problem.

I did have one follow up question or two.  I originally downloaded the Telerik Extensions for ASP.NET MVC from your site but had to download the codeplex version to get the "non-min" JS files.  Should I keep the .min JS files in my project now that I have the uncompressed versions?  Also what does the "Compress(true)" option truly do with the JS files because it doesn't seem to be converting them to .min versions on the fly like I had originally assumed.

Thanks,
Paul
0
Rafael
Top achievements
Rank 1
answered on 08 Nov 2010, 04:14 PM
who could tell me how to assign step for changing value: I mean how to increase/decrease value not on 1,  but on 5 or 20
I appreciate any help
0
Joan Vilariño
Top achievements
Rank 2
answered on 10 Feb 2011, 03:06 PM
Hi... more than half year after, this bug is still there...

I had to patch my telerik.textbox.js to make it work but I would like to get an "official" fixed version, so I don't overwrite the patch everytime there's a new version.

Cheers!
0
Georgi Krustev
Telerik team
answered on 10 Feb 2011, 03:57 PM
Hello,

 
The issue will be fixed in the next official release of the Telerik Components for ASP.NET MVC.
Actually there will be some changes in the rendering which also will fix this issue.

Regards,
Georgi Krustev
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
Joan Vilariño
Top achievements
Rank 2
answered on 10 Feb 2011, 04:25 PM
I hope you don't make too deep changres in rendering .... or I'm sure you'll break my code... today I found one thing that has changed an made misbehave some of my grids.

Before this last version you used 't-grid-action' + 't-grid-edit/delete/whatever' for the grid buttons.

From this last update, the 't-grid-action' class is no more on the grid buttons... so all my $(this).closest('.t-grid-action') stopped working :(

This are the type changes on rendering you are planning for next version?... if so, please document them very well so I can fix my code to work with the newest versions...

0
Georgi Krustev
Telerik team
answered on 10 Feb 2011, 04:44 PM
Hello Joan,

 
All breaking changes will be documented in this help topic, once we release the components.

All the best,
Georgi Krustev
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
Tags
NumericTextBox
Asked by
Paul
Top achievements
Rank 1
Answers by
Alex Gyoshev
Telerik team
Paul
Top achievements
Rank 1
Rafael
Top achievements
Rank 1
Joan Vilariño
Top achievements
Rank 2
Georgi Krustev
Telerik team
Share this question
or