0

I have this regular expression and I need to prevent a password having any of these symbols: !@#$%^&*()_, it is working for !@#$% but not for this password !@#$%D.

$scope.matchPatternPassword = new RegExp("[^!@#$%^&*()_]$");
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Vadim
  • 109
  • 9
  • I read it 5 times and can't understand what you're asking. Can you post more code, or try to phrase it better (It that's google translator, it's getting worse) – Marcos Casagrande Apr 08 '18 at 03:57
  • @MarcosCasagrande here test website a0197766.xsph.ru with right form if push on button my cabinet in password if write !@#$% all it work but so !@#$%D not it work – Vadim Apr 08 '18 at 04:02
  • Ok I believe I got it, password must not contain any of these characters: [^!@#$%^&*()_]$ but `!@#$%D` is working when it shouldn't: In other words: `!@#$%D` **BAD**, yes? – Marcos Casagrande Apr 08 '18 at 04:08
  • @MarcosCasagrande if in string have any from forbidden symbols this means error and user need delete forbidden symbol – Vadim Apr 08 '18 at 04:10
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/168477/discussion-between-vadim-and-marcos-casagrande). – Vadim Apr 08 '18 at 04:16

1 Answers1

2

Your regex was only checking for any of those symbols at the end of the string, that's why the one ending in a letter was working.

The regex should be:

$scope.matchPatternPassword = /^[^!@#$%^&*()_]+$/;

This matches any string that doesn't have any of those characters.

Here's a working example: https://regexr.com/3nh9f

const regex = /^[^!@#$%^&*()_]+$/;

const passwords = [
 '!@#$%',
 '!@#$%D',
 'yes',
 'valid-password',
 'im-valid-too',
 'invalid$',
 'super!-invalid',
 'mypassword'
];

console.log(passwords.filter(password => regex.test(password)));
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98