-2

I need to prevent user from entering the following set of chars: ~ " # % & * : < > ? / \ { | } .

Important is that, anything else will be allowed, but only the characters shown above will be forbidden.

Currently, I'm using the following regex but this does also forbid for example ! sign.

  private static folderRegex = /^[A-Za-z0-9_-]+[ßüÜöÖäÄ\w]*$/;
Ahmet Eroğlu
  • 594
  • 1
  • 7
  • 23

1 Answers1

1

Just put the set of characters in an inverse set and add a ^ and a $ to stretch the pattern to the full length of the input.

 private static folderRegex = /^[^~"#%&*:<>?\/\\{|}]+$/;
skara9
  • 4,042
  • 1
  • 6
  • 21