Solved

Maximum and minum length of textbox

Posted on 2010-09-01
9
1,182 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 40
selected value 16 25
Installing .NET 3.5 on Windows Server 2012 1 59
Jquery if else not working ... WHY? 5 24
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

749 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