0

I have to use regex for my password validation that include special characters at least one.

https://en.wikipedia.org/wiki/ASCII

export const passwordValidation = password => {
  const regPassword = /^(?=.*?[#?!@$%^&*-]).{8,}$/

  return regPassword.test(password)
}

I tried this way but I think this isn't good way. Is there other way to check all special characters by ascii code except alphanumeric ?

kkangil
  • 1,616
  • 3
  • 13
  • 27
  • What are the requirements on your password? – Allan Apr 05 '19 at 07:46
  • Note: All of the characters you've used in your regex's character class are ASCII. – T.J. Crowder Apr 05 '19 at 07:48
  • This will ignore `a-zA-Z0-9` across the `0x20-0x7e` ascii character range. If any of the character is within the range and is not `a-zA-Z0-9`, it'll return a match. `(?![a-zA-Z0-9])[\x20-\x7e]` – josephting Apr 05 '19 at 07:57

1 Answers1

2

First, you need to define what a "special" character is. Do you mean anything not in the range A-Z (English alphabet)? A-Z and 0-9? Something else? Then you either use a character class listing the ones you want, which is what you've done, or a negated class saying you want something other than what's in the class:

return /^(?=.*?[^a-z0-9]).{8,}$/i.test(password);
//              ^---- negated
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875