Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 794 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to limit the number of characters allowed in a textbox?

#11
This did it for me. I did not keep the MultiLine property.

<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" MaxLength="2" Width="100%"></asp:TextBox>
Reply

#12
`MaxLength` does not apply to ASP.NET to Textboxes with `TextMode="MultiLine"`. An easy way to do this **and keep your `MultiLine` mark up** would be to add:

onkeypress="return this.value.length<=10"

with 10 being the limit. Like so:

<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" onkeypress="return this.value.length<=10" TextMode="MultiLine" Width="100%"></asp:TextBox>

 

**EDIT Option One (Server side)** The above example has some issues pointed out over the years. The underlying problem is a multi-line `textBox` is rendered as a text-area and `MaxLength` is removed. We can also fix in the C# or VB asp.net code behind of the page by forcing the re-attachment of `MaxLength` instead of attaching `onkeypress`.

protected void Page_Load(object sender, EventArgs e)
{
txtBodySMS.Attributes.Add("maxlength", "10");
}

**EDIT Option Two (Client side):** Here's a simple jquery solution. Include the following script in your head instead of attaching `onkeypress` or editing the server side code.


$(document).ready(function () {
$(".MultiLineLimit").on('change keydown paste input', function () {
this.value = (this.value.length <= 10 ? this.value : this.value.substring(0, 10));
});
});


Now just add the `MultiLineLimit`class to any of your `<asp:TextBox>` textbox of `TextMode="MultiLine"`.

<asp:TextBox ID="txtBodySMS" CssClass="MultiLineLimit" runat="server" Rows="10" TextMode="MultiLine" Width="100%"></asp:TextBox>

Remember in either scenario client side validation can be removed. If your storing this in a database it should always be validated server side as well.
Reply

#13
It seems to be a curious oversight in the design of ASP.NET (leaving aside the anomaly with textareas) that you can set the MaxLength property of a textbox but there seems to be no way provided to enforce this limit server-side without explicitly adding a validator. That's a nuisance if you have lots of fields and just want to be sure that no-one is subventing the limits of the form by injecting their own POST data. So I wrote this, which seems to do the trick - obviously you might not want to force Response.End

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

Dim colControls As Collection = New Collection
getControlList(colControls, Me)
For Each ctlNextPlease As Control In colControls
If TypeOf (ctlNextPlease) Is TextBox Then
Dim ctlTextBox As TextBox = ctlNextPlease
If Len(Request(ctlTextBox.ID)) > ctlTextBox.MaxLength Then
Response.Write("The value <i>" & Request(ctlTextBox.ID) & "</i> submitted for <b>" & ctlTextBox.ID & "</b> exceeds the permitted maximum length (" & ctlTextBox.MaxLength & ") for that value")
Response.End()
End If
End If
Next
...
End Sub

Public Shared Sub getControlList(ByRef colControls As Collection, ByVal rootControl As Control)
colControls.Add(rootControl)
If rootControl.Controls.Count > 0 Then
For Each controlToSearch As Control In rootControl.Controls
getControlList(colControls, controlToSearch)
Next
End If
End Sub
Reply

#14
```xml
<asp:TextBox ID="txtBodySMS" runat="server" Rows="10" MaxLength="220"
TextMode="MultiLine" Width="100%">
</asp:TextBox>
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through