Solved

Regular expression for Asp.net text box conrol

Posted on 2010-11-22
8
584 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
  • 2
  • 2
  • 2
  • +1
8 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Why not use the MaxLength=255 property of textbox
0
 
LVL 20

Expert Comment

by:informaniac
Comment Utility
Sry. missed out the multine thing..
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 29

Expert Comment

by:Kumaraswamy R
Comment Utility
<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
Comment Utility
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
Comment Utility
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
Comment Utility
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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