Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to limit string length for strings with new line characters

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

like image 998
Andrey Avatar asked Oct 23 '09 20:10

Andrey


1 Answers

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.

like image 131
Yannick Motton Avatar answered Sep 29 '22 12:09

Yannick Motton