8

I need to validate the length of input of a textbox.

The max length property does not work for Multiline textboxes.

The regular expression I have is:

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server"
                            ErrorMessage="The notes has exceeded maximum length."
                            ControlToValidate="txtNotes" Display="Dynamic"
                            ValidationExpression=".{0,500}" ValidationGroup="PO">
                            *</asp:RegularExpressionValidator>

The problem I'm having is when a new line is entered into the textbox the validator reports a problem.

What would I need to add to the ValidationExpression to ignore carriage returns?

Thanks

Michael Myers
  • 188,989
  • 46
  • 291
  • 292
gazamatazzer
  • 430
  • 6
  • 14

1 Answers1

12

Change .{0,500} to [\s\S]{0,500}. Note that carriage returns won't exactly be ignored; they'll still count toward the 500-character limit.

Of course, if 500 characters is the size of your database field, that's exactly the behavior you want.

Justin Morgan - On strike
  • 30,035
  • 12
  • 80
  • 104