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

Last Reply 11 months ago By dharmendr

Posted 11 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?

You are viewing reply posted by: dharmendr 11 months ago.
Posted 11 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