Solved

Maximum and minum length of textbox

Posted on 2010-09-01
9
1,164 Views
Last Modified: 2013-12-17
How to set the maximum and minimum length of textbox...
Client side not server side...
0
Comment
Question by:PagodNaUtak
9 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33584141
0
 
LVL 4

Expert Comment

by:thepanch
ID: 33584182
maximum length displayed at screen
size=number
maximum characters to receive on the textbox
maxlength=number
0
 
LVL 5

Expert Comment

by:kosturdur
ID: 33584190
Hi,
Solutions from
http://geekswithblogs.net/mahesh/archive/2007/12/27/asp.net-textbox.multiline-maxlength.aspx
with little changes;

Solution Number 1 for
ASP.NET TextBox.MultiLine maxlength
Add following javascript

<script type="text/javascript" language="javascript">
/* <![CDATA[ */
function Count(text,long)
{
   var maxlength = new Number(long); // Change number to your max length.
  if(document.getElementById('<%=textBox.ClientID%>').value.length > maxlength)
  {
     text.value = text.value.substring(0,maxlength);
     alert(" Only " + long + " chars");
}
/* ]]> */
</script>
 
Where “textBox” is the asp text box ID.
Also add following events in your textbox.

onKeyUp="javascript:Count(this,200);" onChange="javascript:Count(this,200);"

Your textbox code should look like
<asp:TextBox ID="textBox" onKeyUp="javascript:Count(this,2);" onChange="javascript:Count(this,2);"  TextMode=MultiLine Columns="5" Rows="5" runat=server>
    </asp:TextBox>
 
Solution Number 2 for
ASP.NET TextBox.MultiLine maxlength
Another way to achieve this is regular expression. You can add following regular expression validate on asp text box.

<asp:RegularExpressionValidator ID="txtConclusionValidator1" ControlToValidate="textBox" Text="Exceeding 200 characters" ValidationExpression="^[\s\S]{0,2}$" runat="server" />

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33584199
Creating a TextBoxLengthValidator Validator Control
http://www.4guysfromrolla.com/articles/112404-1.aspx
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 6

Expert Comment

by:Neeraj Soni
ID: 33584235
you can add MaxLength=10 which will restrict entering char after 10 chars in txtbox.

You have to add validator on txtbox for minlength.
0
 
LVL 8

Author Comment

by:PagodNaUtak
ID: 33584268
@neerajsoni,
Max length does not work in multiline textbox...
0
 
LVL 4

Accepted Solution

by:
thepanch earned 500 total points
ID: 33584279
no it doesnt because it only allows one line, so if you are triying to limit the character count (enter key counts as one) you'll have to do as kosturdur says with the count function.
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33584598
you can use regexvalidator to define min and maxlength {minlength,maxlength}

like

^[a-zA-Z.]{2,30}$
0
 
LVL 5

Expert Comment

by:kosturdur
ID: 33584614
I need the points man to ask some question :(
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now