Set "maxlength" to a field in forms

2 Answers 1218 Views
Form TextBox
Humberto
Top achievements
Rank 1
Iron
Humberto asked on 15 Feb 2022, 02:55 PM

Hi,

I'm trying to set the maximum length of input characters using HtmlAttributes in a form. But it seems it doesn't work; I can still type more than 4 characters. Here is an example in REPL:

https://netcorerepl.telerik.com/wmkmbflo51nxiZPb01


                   i.Add()
                        .Field(f => f.FirstName)
                        .Editor(e=> e.TextBox().HtmlAttributes(new { maxlength = "4" }))
                        .Label(l => l.Text("First Name:"));

Thanks,

Humberto

 

2 Answers, 1 is accepted

Sort by
0
Alexander
Telerik team
answered on 18 Feb 2022, 08:50 AM

Hi Humberto,

Thank you for the provided information, code snippet, and example:

In order to apply attributes to the input element of a specific item within the .Items() configuration, you can use the built-in .InputHtmlAttributes() configuration option instead.

Here is an example:

        .Items(items =>
        {
            items.AddGroup()
                .Label("Registration Form")
                .Items(i =>
                {
                    i.Add()
                        .Field(f => f.LastName)
                        .InputHtmlAttributes(new { maxlength =3 })
                        .Label(l => l.Text("Last Name:"));

                });
        })

In addition, I have also updated the previously sent Telerik REPL example with the updated configuration for your convenience.

Give this suggestion a try and let me know how it works out for you.

Regards,
Alexander
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Humberto
Top achievements
Rank 1
Iron
answered on 18 Feb 2022, 12:03 PM

Hi Alexander,

It works, thanks for the answer.

Regards,

Humberto

Tags
Form TextBox
Asked by
Humberto
Top achievements
Rank 1
Iron
Answers by
Alexander
Telerik team
Humberto
Top achievements
Rank 1
Iron
Share this question
or