Maximum and minum length of textbox

How to set the maximum and minimum length of textbox...
Client side not server side...
LVL 8
PagodNaUtakAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
thepanchConnect With a Mentor Commented:
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
 
Gurvinder Pal SinghCommented:
0
 
thepanchCommented:
maximum length displayed at screen
size=number
maximum characters to receive on the textbox
maxlength=number
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
kosturdurCommented:
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
 
DhaestCommented:
Creating a TextBoxLengthValidator Validator Control
http://www.4guysfromrolla.com/articles/112404-1.aspx
0
 
Neeraj SoniSr. ArchitectCommented:
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
 
PagodNaUtakAuthor Commented:
@neerajsoni,
Max length does not work in multiline textbox...
0
 
puru1981Commented:
you can use regexvalidator to define min and maxlength {minlength,maxlength}

like

^[a-zA-Z.]{2,30}$
0
 
kosturdurCommented:
I need the points man to ask some question :(
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.