Click here to Skip to main content
11,483,003 members (73,906 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 31-Jan-11 1:25am
Comments
Member 9702948 at 4-Oct-13 8:33am
   
Nice solution thanks..!!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You cannot do this directly using the maxLength property.
However, Javascript can help you achieve this[^].
  Permalink  
Comments
Pravin Patil, Mumbai at 31-Jan-11 7:00am
   
Nice Link...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v3
Comments
Pravin Patil, Mumbai at 31-Jan-11 7:00am
   
I think this is an easier version of what abhinav has posted...
But both work correctly..
NOORULLA KHAN at 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 at 28-Dec-11 3:43am
   
great post.. it trully helps! thnks
sharadpanwal at 31-Aug-12 7:40am
   
Really helpfull!Thanks
nomi ali 9054 at 27-Feb-13 6:45am
   
its not working if i do copy-paste.
Prerak Patel at 27-Feb-13 7:14am
   
Show your tag
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Use Regular Expression Validator . As per below example, max text box length is 1000.
<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" />
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You can use javascript

<asp:TextBox ID="TextBox1"  onchange="MaxLength(5)" runat="server" TextMode="MultiLine" > </asp:TextBox>
<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>
  Permalink  
Comments
Abhinav S at 31-Jan-11 6:33am
   
Looks like we posted similar answers together. :)
mkgoud at 31-Jan-11 8:14am
   
Yes posted almost at same time.Ah! But why did my post did a get two vote.
Manas Bhardwaj at 31-Jan-11 8:28am
   
+5
Abhijit Jana at 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 !
mkgoud at 31-Jan-11 23:58pm
   
Yes sir better practice.
Member 8158717 at 31-Aug-12 6:07am
   
This code is processed only after the post back. Is there any way in which javascript code execute whenever the Tab button on keyboard is pressed
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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..
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 10

[^][^][^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

save property as follows..

<asp:textbox id="txtdays" runat="server" cssclass="numerictxtbox" height="16px" width="21px" maxlength="2" xmlns:asp="#unknown">
  Permalink  
Comments
CHill60 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

In 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:TextBox runat="server" ID="txtAdmissionDate" Width="450px" TextMode="MultiLine"
            onKeyUp="CheckTextLength(this,10)" onChange="CheckTextLength(this,10)"></asp:TextBox>
  Permalink  

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



Advertise | Privacy | Mobile
Web04 | 2.8.150520.1 | Last Updated 4 Dec 2014
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100