Regex to limit string length for strings with new line characters

asp.net, regex, validation

Solution

You could use the `RegexOptions.Singleline` option when validating input. This treats the input as a single line statement, and parses it as such.

Otherwise you could give the following expression a try:

^(.|\s){1,500}$

This should work in multiline inputs.

Problem

Looks like a simple task - get a regex that tests a string for particular length: ^.{1,500}$ But if a string has "\r\n" than the above match always fails! How should the correct regex look like to accept new line characters as part of the string? I have a `<asp:TextBox TextMode="Multiline">` and use a RegularExpressionValidator to check the length of what user types in. Thank you, Andrey

Original source