Click here to Skip to main content
15,669,524 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use a content page. In the following code I want to limit the characters to 200.
It is allowing to enter more than that. How to restrict it.

<asp:TextBox ID="TBMessage" runat="server"  OnTextChanged="TBMessage_TextChanged" AutoPostBack="True" MaxLength="200"
               style="z-index: 1; top: 485px; left: 600px; position: absolute; resize:none ; height: 233px; width: 400px"

Share this answer
MaxLength will not work with a MutliLine textmode setting.
To handle such scenarios a custom validation scenario with RegularExpressionValidator needs to be built.
An example would be ValidationExpression="^(\w*)(\s*)(.*){15}$" (15 would be length).
Share this answer
VICK 20-Mar-14 0:51am    
MY 5+. :)
Abhinav S 20-Mar-14 1:56am    
Aarti Meswania 20-Mar-14 2:37am    
5+! :)
Abhinav S 20-Mar-14 3:58am    
Aarti Meswania 20-Mar-14 4:23am    
welcome :)
Try this, the max len in this example is 10:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default23.aspx.cs" Inherits="Default23" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
    <script type="text/javascript">
        function enforceMaxLen(field, maxlen) {
            if (field.value.length > maxlen)
                field.value = field.value.substring(0, maxlen);
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"
         onkeydown="enforceMaxLen(TextBox1, 10);"
         onkeyup="enforceMaxLen(TextBox1, 10);"
        Style="height: 250px; width: 300px" TextMode="MultiLine"></asp:TextBox>

It works even if you try to copy and paste more than the maxlen.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900