Solved

Regular expression for Asp.net text box conrol

Posted on 2010-11-22
8
595 Views
Last Modified: 2012-05-10

I have a text box control and I need to restrict the text box input to no more than 255 characters. I created
a regular expression, however it is not correct. Can someone help me out? Here is my validation expression:
validationexpression="^.{0,255}$">


My actual Asp.net code looks something like, this but I know that the regular expression is not correct.

<ASP:RegularExpressionValidator id="regValInstructions" runat="server"
controltovalidate="txtMessage" textmode="multiline" tabindex="43"></ASP:TextBox>



validationexpression="^.{0,255}$">
<ASP:RegularExpressionValidator>
0
Comment
Question by:brgdotnet
[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
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 200 total points
ID: 34193754
The expression looks fine to me... shouldn't the code look like this though? (or was it just copying and pasting that jumbled it up?)

Try:
validationexpression="^[\s\S]{0,255}$">
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 34193756
Sorry... ignore the 1st line of my above comment. I meant to erase that and just suggest the pattern.
0
 
LVL 20

Expert Comment

by:informaniac
ID: 34193913
Why not use the MaxLength=255 property of textbox
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:informaniac
ID: 34193917
Sry. missed out the multine thing..
0
 
LVL 29

Expert Comment

by:Kumaraswamy R
ID: 34194014
<asp:RegularExpressionValidator ID="txtConclusionValidator1" ControlToValidate="textBox" Text="Exceeding 255 characters" ValidationExpression="^[\s\S]{0,255}$" runat="server" />
0
 
LVL 29

Assisted Solution

by:Kumaraswamy R
Kumaraswamy R earned 40 total points
ID: 34194025
ASP.NET TextBox.MultiLine maxlength
-----------------------------------
Add following javascript

function Count(text,long)
 
{
var maxlength = new Number(long); // Change number to your max length.
if(document.getElementById('<%=TXTCount.ClientID%>').value.length > maxlength){
   text.value = text.value.substring(0,maxlength);
   alert(" Only " + long + " chars");
}
 


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="TXTCount" onKeyUp="javascript:Count(this,2);" onChange="javascript:Count(this,2);"  TextMode=MultiLine Columns="5" Rows="5" runat=server />
   
0
 
LVL 9

Expert Comment

by:abhinayp86
ID: 34196983
You can find many Regular Expressions here.. Just do a search in it

http://regexlib.com/Search.aspx?k=limit
0
 
LVL 9

Expert Comment

by:abhinayp86
ID: 34197022
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…

738 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