Validating multiline text input using .NET.

I have an HtmlTextArea and I want to limit the number of characters that users can enter up to 500.

I have currently used RegularExpressionValidator ...

RegularExpressiondValidator val = new RegularExpressiondValidator ();
val.ValidationExpression = "^.{0,500}$";
val.ControlToValidate = id;
val.ErrorMessage = "blah";

... this is great when the text is entered on one line, but it instantly fails to check whenever the text contains a new string character (i.e. multi-line).

I understand that there are different regex mechanisms, and I need to test .NET. (can someone point me in the direction of good online?), but I tried a couple of other things, including guesses "(? m)" in my expression string and replacing ^ and $ with \ A and \ Z, but still bad luck.

Another related question is, can I not use the regex at all and associate this validator with my own validation function somehow?

+5
source share
3 answers

"." does not include newlines.

^ ((|. \ N) {0500}) $

This page may also help; this is a remarketing sheet.

+6
source

Change your regex to the following

"^(.|\n){0,500}$";

. the character matches all but a new line. Suggest or fix your problem.

+6
source

: javascript , jQuery ( javascript).

$(document).ready(function(){
  var $TextBox = $("#<%=TextBox.ClientID %>");

  $TextBox.keyup(function(){
     return $TextBox.length <= <%=TextBox.MaxLength %>;
  });
});

Custom Validator .

There are also ways to enable and disable ASP.Net validators using javascript so you can also learn this.

+1
source

All Articles