Fatemeh Namkhah
Fatemeh Namkhah

Reputation: 711

pattern password javascript

I'm working on a pattern for a password with the following requirements:

  1. Min character = 6
  2. Max character = 64
  3. Min 1 lowercase character
  4. Min 1 uppercase character
  5. Min 1 number
  6. Min 1 special characters

I am using this regex:

var passReg = /^(?=^[ -~]{6,64}$)(?=.*([a-z][A-Z]))(?=.*[0-9])(.*[ -/|:-@|\[-`|{-~]).+$/;

However, it does not work as expected.

Upvotes: 2

Views: 5719

Answers (3)

ghiles ybeggazene
ghiles ybeggazene

Reputation: 275

Input Password and Submit [8 to 25 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character]

 /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,25}$/;

Upvotes: 0

Navidot
Navidot

Reputation: 325

Are consider special non-whitespace characters. I think this is complite list:

! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~

Try this one:

var passReg = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!"#$%&'()*+,-.\/:;<=>?\\@[\]^_`{|}~]).{6,64}$/;

Look at back reference for special characters. In character sets chars like \ and ] must be escaped.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

You must be looking for this regex:

^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[ -/:-@\[-`{-~]).{6,64}$

See demo

Here is explanation:

  • ^ - Beginning of string
  • (?=.*[a-z]) - A positive look-ahead to require a lowercase letter
  • (?=.*[A-Z]) - A positive look-ahead to require an uppercase letter
  • (?=.*[0-9]) - A positive look-ahead to require a digit
  • (?=.*[ -/:-@\[-{-~])` - A positive look-ahead to require a special character
  • .{6,64} - Any character (but a newline), 6 to 64 occurrences
  • $ - End of string.

Upvotes: 2

Related Questions