DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Set Maxlength For TextArea In ASP.NET

  • submit to reddit
        Below given is the code to set maxlength for text area in

Note: txtMyTextBox is the name of the text area. This has to placed in page load event

Const LENGTH_TEXT As Integer = 100

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  Dim lengthFunction As String = "function isMaxLength(txtBox) {"
  lengthFunction += " if(txtBox) { "
  lengthFunction += " return ( txtBox.value.length <=" & LENGTH_TEXT & ");"
  lengthFunction += " }"
  lengthFunction += "}"
  Me.txtMyTextBox.Attributes.Add("onkeypress", "return isMaxLength(this);")
  ClientScript.RegisterClientScriptBlock(Me.[GetType](), "txtLength", lengthFunction, True)

End Sub