Search code examples
javascriptregexvalidationpasswords

How to escape * in regex password pattern


I use validate.js for form validation and have a password pattern where I would like to allow only one of the following special characters: -!@_#+

Password pattern is as below:

 password1: {
  pattern:/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[-!@_#+])(?=.{8,})/
},

However, when I enter *, $ or % character as password input no error message is given. Any help would be appreciated. Thank you.


Solution

  • You are not really limiting the input options of the user, your pattern says: From the starting position of string, check if it is followed by a lower case letters, then check if it is followed by a uppercase letters, followed by numbers and then some symbols.

    There is nothing to limit the users input. Since .* will match any char that is not a line terminator(\n, \r etc).

    You should try something like this: /^([-\w!@#+]+){8,}$/
    Here \w mathces a-z, A-Z, 0-9 and underscore.

    Test regex here: https://regex101.com/r/TINm56/1

    enter image description here