Click here to Skip to main content
15,878,970 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.

XML
<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"
               TextMode="MultiLine"></asp:TextBox>
Posted

 
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
 
Comments
VICK 20-Mar-14 0:51am    
MY 5+. :)
Abhinav S 20-Mar-14 1:56am    
Thanks.
Aarti Meswania 20-Mar-14 2:37am    
5+! :)
Abhinav S 20-Mar-14 3:58am    
Thanks.
Aarti Meswania 20-Mar-14 4:23am    
welcome :)
Try this, the max len in this example is 10:
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default23.aspx.cs" Inherits="Default23" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript">
        function enforceMaxLen(field, maxlen) {
            if (field.value.length > maxlen)
                field.value = field.value.substring(0, maxlen);
        }
    </script>
</head>
<body>
    <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>
    </form>
</body>
</html>

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

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