ASP.Net RegularExpression Validators
Maximum character length Validation (Maximum 6 characters allowed)
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1" Display = "Dynamic" ControlToValidate = "TextBox1" ValidationExpression = "^[\s\S]{0,6}$" ErrorMessage="Maximum 6 characters allowed."> </asp:RegularExpressionValidator>
Minimum character length Validation (Minimum 6 characters required)
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator2" Display = "Dynamic" ControlToValidate = "TextBox2" ValidationExpression = "^[\s\S]{6,}$" ErrorMessage="Minimum 6 characters required."> </asp:RegularExpressionValidator>
Minimum and Maximum character length Validation (Minimum 2 and Maximum 6 characters required)
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator3" Display = "Dynamic" ControlToValidate = "TextBox3" ValidationExpression = "^[\s\S]{2,6}$" ErrorMessage="Minimum 2 and Maximum 6 characters required."> </asp:RegularExpressionValidator>
Alphanumeric with special Characters
Minimum length 7 and Maximum length 10. Characters allowed – a – z A – Z 0-9 ’@ & # .
<asp:RegularExpressionValidator ID="RegExp1" runat="server" ErrorMessage="Password length must be between 7 to 10 characters" ControlToValidate=" txtPassword " ValidationExpression="^[a-zA-Z0-9'@&#.\s]{7,10}$" />
Alphanumeric Only
Minimum length 7 and Maximum length 10. Characters allowed – a – z A – Z 0-9
<asp:RegularExpressionValidator ID="RegExp1" runat="server" ErrorMessage="Password length must be between 7 to 10 characters" ControlToValidate=" txtPassword " ValidationExpression="^[a-zA-Z0-9\s]{7,10}$" />
Alphabets Only
Minimum length 7 and Maximum length 10. Characters allowed – a – z A – Z
<asp:RegularExpressionValidator ID="RegExp1" runat="server" ErrorMessage="Password length must be between 7 to 10 characters" ControlToValidate="txtPassword" ValidationExpression="^[a-zA-Z]{7,10}$" />
Numeric Only
Minimum length 7 and Maximum length 10. Characters allowed – 0 – 9
<asp:RegularExpressionValidator ID="RegExp1" runat="server" ErrorMessage="Password length must be between 7 to 10 characters" ControlToValidate="txtPassword" ValidationExpression="^[0-9]{7,10}$" />
source:
http://www.aspsnippets.com/Articles/ASP.Net-Regular-Expression-Validator-to-validate-Minimum-and-Maximum-Text-Length.aspx