Solved

Maximum and minum length of textbox

Posted on 2010-09-01
9
1,185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 53

Expert Comment

by:Dhaest
ID: 33584199
Creating a TextBoxLengthValidator Validator Control
http://www.4guysfromrolla.com/articles/112404-1.aspx
0
 
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 Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

732 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