Limiting the length of a multiline textbox - Raj Kaimal

Limiting the length of a multiline textbox

Limiting the length of an ASP.net mulitline textbox control is easy. Add a RegularExpressionValidator, set the ControlToValidateProperty to the ID of the TextBox you wish to validate and set the ValidationExpression property to :

^[\s\S]{0,300}$

This tells the regex validator to limit the number of characters in the Textbox to 300.

Metacharacters

^ Start of Line
[] Character class (list the characters you want to match)
\s White space characters including new line
\S Non-white space characters
{intMin,intMax} Intervals; The minimium number of matches you want to require and the max number of matches you want to allow

 

Published Thursday, August 02, 2007 3:01 PM by rajbk
Filed under:

Comments

# re: Limiting the length of a multiline textbox

Why not us he dot (.) instead of the couple \s\S. This stands for "any character".

The result: ^.{0,300}$

Or did I miss something ?

Friday, August 03, 2007 1:37 AM by Sebastien Ros

# re: Limiting the length of a multiline textbox

Sebastien,

. stands for any character excluding the line break characters. \s includes the new line.

Friday, August 03, 2007 8:38 AM by rajbk

# re: Limiting the length of a multiline textbox

Very useful.  Thanks!

Tuesday, August 07, 2007 2:03 PM by Dan

# re: Limiting the length of a multiline textbox

Thank you, this is exactly what I needed.

Thursday, October 18, 2007 5:33 PM by Seth

# re: Limiting the length of a multiline textbox

Execellent! Could not be any more simple

Wednesday, April 09, 2008 1:18 PM by Ben

# re: Limiting the length of a multiline textbox

Very Helpfull

Saturday, July 26, 2008 3:19 AM by Prakash

# re: Limiting the length of a multiline textbox

very useful.Thank u

Wednesday, August 06, 2008 11:38 PM by sowjanya

Leave a Comment

(required) 
(required) 
(optional)
(required)