Posts Tagged ‘regular expression’

ASP.NET max length validator

Written by Alex on . Posted in Uncategorized

ASP.NET has no build in validator for textbox or multiline textbox length validation. But it can be done using RegularExpressionValidator validator.

Using RegularExpressionValidator to validate max length in ASP.NET

To validate max length using regular expression you can use this regular expression pattern

(\s|.){0,1024}$

it matches text length from 0 to 1024 and if text length is more than 1024 this validator display error message to user.

Source code of ASP.NET max length validator

<asp:textbox id="txtProjectInfo" runat="server" Rows="4" TextMode="MultiLine"></asp:textbox>
<asp:RegularExpressionValidator id="maxLenProjectInfo" ControlToValidate="txtProjectInfo"
ValidationExpression="(\s|.){0,1024}$"
ErrorMessage="Project info is too long. Max length is 1024." runat="server"/>