Regular Expression to allow Enter Key in MultiLine TextBox in ASP.Net

Last Reply 6 months ago By dharmendr

Posted 6 months ago

Sir,

I have Multiline textbox with regex for alphabet and max 300 char length.

Should allow line break in multiline textbox if user press enter key. Below is my regex. 

<asp:RegularExpressionValidator Display="Dynamic" ID="RegularExpressionValidator1txtaddress" ControlToValidate="txtaddress" runat="server" ErrorMessage="Only alphabet & space allowed. Max. 300 chars" ValidationGroup="reg" CssClass="valmsg" ValidationExpression="[a-zA-Z ]{1,300}$"></asp:RegularExpressionValidator>

How should I do this?

Posted 6 months ago

HiSumeet,

You should add special character for new line into regex pattern.

Check this example. Now please take its reference and correct your code.

HTML

<asp:TextBox runat="server" ID="txtaddress" TextMode="MultiLine" />
<asp:RegularExpressionValidator Display="Dynamic" ID="RegularExpressionValidator1txtaddress"
    ControlToValidate="txtaddress" runat="server" ErrorMessage="Only alphabet & space allowed. Max. 300 chars"
    ValidationGroup="reg" CssClass="valmsg" ValidationExpression="[a-zA-Z \r\n]{1,300}$"></asp:RegularExpressionValidator>
<asp:Button Text="Save" runat="server" ValidationGroup="reg" />

Screenshot