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("[^!@#$%^&*()_]$");
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("[^!@#$%^&*()_]$");
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)));