1

I need to only allow following characters in multi-line textarea input field:

space, numbers, English letters and following special characters: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ ` [ \ ~ ] ^ _ { | }

I figured it out except the multi-line part:

        [RegularExpression("^[ -~]+$", ErrorMessage = @"Allowed characters for item description: space, numbers, English letters and following special characters: ! "" # $ % & ' ( ) * + , - . / : ; < = > ? @ ` [ \ ~ ] ^ _ {{ | }}")]

It works as desired as long as everything is entered as a single line. I just can't figure out how to extend it to multi-line.

5
  • Does the tested value contain more than one line? Commented Jun 4, 2018 at 18:52
  • 1
    Try "^[ -~]+(?:\r?\n[ -~]+)*$" Commented Jun 4, 2018 at 18:55
  • @WiktorStribiżew: this is almost it. It works except if the very first or the very last line is an empty line Commented Jun 4, 2018 at 19:07
  • 1
    @JoeSchmoe What does that mean? That lines me be empty? Then use "^[ -~]*(?:\r?\n[ -~]*)*$" Commented Jun 4, 2018 at 19:10
  • @WiktorStribiżew: your second version seems to be working 100%. What I meant before: if I enter "ABC[Enter]DEF[Enter]" then it would not valdiate but now it does. If you post this as an answer I will mark it as such. Commented Jun 4, 2018 at 19:15

1 Answer 1

1

You may use

"^[ -~]*(?:\r?\n[ -~]*)*$"

The pattern matches:

  • ^ - start of string
  • [ -~]* - 0 or more printable ASCII chars
  • (?: - start of a non-capturing group matching
    • \r?\n - an optional (1 or 0) CR symbols and then an LF symbol (so that it matches Windows and Unix/Linux line endings)
    • [ -~]* - 0 or more printable ASCII chars
  • )* - ... zero or more times
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.