In this article I will explain with an example, how to validate minimum and maximum character length in TextBox control using RegularExpression validator in ASP.Net.
 
 

Maximum character length Validation (Maximum 8 characters allowed)

The following HTML Markup consists of:
Label – For displaying message.
TextBox – For capturing user input.
RegularExpressionValidator – For validating control.

Properties

ControlToValidate – For providing validation property to the Control.
ValidationExpression – For specifying the expression. (Here it is specified Maximum 8 characters)
<b><asp:Label ID="Label1" runat="server" Text="Maximum character length Validation (Maximum 8 characters allowed)"></asp:Label></b>
<br /><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" Display="Dynamic" ControlToValidate="TextBox1"
    ValidationExpression="^[\s\S]{0,8}$" ForeColor="Red" runat="server" ErrorMessage="Maximum 8 characters allowed."></asp:RegularExpressionValidator>
 
 

Minimum character length Validation (Minimum 8 characters required)

The following HTML Markup consists of:
Label – For displaying message.
TextBox – For capturing user input.
RegularExpressionValidator – For validating control.

Properties

ControlToValidate – For providing validation property to the Control.
ValidationExpression – For specifying the expression. (Here it is specified Minimum 8 characters)
<b><asp:Label ID="Label2" runat="server" Text="Minimum character length Validation (Minimum 8 characters required)"></asp:Label></b>
<br /><br />
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" Display="Dynamic" ControlToValidate="TextBox2"
    ValidationExpression="^[\s\S]{8,}$" ForeColor="Red" runat="server" ErrorMessage="Minimum 8 characters required."></asp:RegularExpressionValidator>
 
 

Minimum and Maximum character length Validation (Minimum 5 and Maximum 8 characters required)

The following HTML Markup consists of:
Label – For displaying message.
TextBox – For capturing user input.
RegularExpressionValidator – For validating control.

Properties

ControlToValidate – For providing validation property to the Control.
ValidationExpression – For specifying the expression. (Here it is specified Minimum 5 and Maximum 8 characters)
<b><asp:Label ID="Label3" runat="server" Text="Minimum and Maximum character length Validation (Minimum 5 and Maximum 8 characters required)"></asp:Label></b>
<br /><br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator3" Display="Dynamic" ControlToValidate="TextBox3"
    ValidationExpression="^[\s\S]{5,8}$" ForeColor="Red" runat="server" ErrorMessage="Minimum 5 and Maximum 8 characters required."></asp:RegularExpressionValidator>
 
 

Screenshot

TextBox: Minimum and Maximum Character Length Validation using ASP.Net RegularExpression Validators
 
 

Demo

 
 

Downloads