Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
How to limit characters of textbox with multiline property.I use MaxLength
property but not working.give me some idea or any solution.can I use javascript?
Posted
Comments
che2chetangiri 4-Oct-13 8:33am    
Nice solution thanks..!!

You cannot do this directly using the maxLength property.
However, Javascript can help you achieve this[^].
 
Share this answer
 
Comments
Pravin Patil, Mumbai 31-Jan-11 7:00am    
Nice Link...
 
Share this answer
 
Alternatively you can directly specify this in your textbox tag.
onkeypress="return this.value.length<=10" //replace 10 with the max length you want


This exactly works as MaxLength.
 
Share this answer
 
v3
Comments
Pravin Patil, Mumbai 31-Jan-11 7:00am    
I think this is an easier version of what abhinav has posted...
But both work correctly..
NOORULLA KHAN 2-Sep-13 7:53am    
Exactly It works but once if you Insert example 10 char.then Multiple textbox will stop adding some more extra char. but if you Press back button or delete then It won't works .how to make it easy or how to Solve this Problem
D K N T H 28-Dec-11 3:43am    
great post.. it trully helps! thnks
sharadpanwal 31-Aug-12 7:40am    
Really helpfull!Thanks
nomi ali 27-Feb-13 6:45am    
its not working if i do copy-paste.
Use Regular Expression Validator . As per below example, max text box length is 1000.
CSS
<asp:TextBox ID="txtDescription" Width="300" Height="50" TextMode="MultiLine" runat="server"
                      Text="" />
                  <asp:RegularExpressionValidator ID="regexpDesc" runat="server" ErrorMessage="Maximum of 1000 characters allowed"
                      ControlToValidate="txtDescription" Display="Dynamic" ValidationExpression=".{0,1000}"
                      ValidationGroup="NewValidationGroup" />
 
Share this answer
 
You can use javascript

<asp:TextBox ID="TextBox1"  onchange="MaxLength(5)" runat="server" TextMode="MultiLine" > </asp:TextBox>

XML
<script language="javascript" type="text/javascript">
function MaxLength(maxLength)
{
text=document.getElementById('TextBox1');
if(text.value.length>maxLength)
{
alert("only max " + maxLength + " characters are allowed");
//this limits the textbox with only 5 characters as lenght is given as 5.
text.value = text.value.substring(0,maxLength);
}
}
</script>
 
Share this answer
 
Comments
Abhinav S 31-Jan-11 6:33am    
Looks like we posted similar answers together. :)
m@dhu 31-Jan-11 8:14am    
Yes posted almost at same time.Ah! But why did my post did a get two vote.
Manas Bhardwaj 31-Jan-11 8:28am    
+5
Abhijit Jana 31-Jan-11 9:11am    
I will prefer Validator ( Obviously based on situation ) , because if I have 10 control and I have to do some group control validation, then it will create problem. Hope you got my point. BTW : it's a good answer !
m@dhu 31-Jan-11 23:58pm    
Yes sir better practice.
One more better way, you can use regex for this purpose. Use regular expression validator with validate expression
^[a-zA-Z.]{0,30}
also set the error message in validator, it will work for you..
 
Share this answer
 
[^][^][^]
 
Share this answer
 
save property as follows..

<asp:textbox id="txtdays" runat="server" cssclass="numerictxtbox" height="16px" width="21px" maxlength="2" xmlns:asp="#unknown">
 
Share this answer
 
Comments
CHill60 11-Jun-13 11:09am    
This question is over 2 years old and already resolved. Also the OP stated that he had tried the maxlength property but it didn't work
In Javascript,-
JavaScript
<script language="javascript" type="text/javascript">
       
        function CheckTextLength(text, long) {
    var maxlength = new Number(long); // Change number to your max length.
    if (text.value.length > maxlength) {
        text.value = text.value.substring(0, maxlength);
        alert(" Only " + long + " characters allowed");
    }
}
    </script>


In Aspx page ,-
ASP.NET
<asp:TextBox runat="server" ID="txtAdmissionDate" Width="450px" TextMode="MultiLine"
          onKeyUp="CheckTextLength(this,10)" onChange="CheckTextLength(this,10)"></asp:TextBox>
 
Share this answer
 
<asp:textbox id="txtdetal" runat="server" textmode="MultiLine" onkeypress="return this.value.length<=90;" height="75px" width="300px" xmlns:asp="#unknown">

here u can replace 90 to your max character value.
 
Share this answer
 
Comments
CHill60 21-Jun-15 19:16pm    
Question is over 4 years old! ... and already resolved.

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