Write Regex Pattern for Password Validation Like a Pro
Tags: regex, formvalidation, javascript, webdev
Published: 2022-06-20 11:40:20


### `✅The following 4 regex patterns can help you to write almost any password validation`

&nbsp;


&nbsp;

### Pattern 1:

&nbsp;




> Password must contain one digit from 1 to 9, one lowercase letter, one uppercase letter, one special character, no space, and it must be 8-16 characters long.


```js
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*\W)(?!.* ).{8,16}$/
```

&nbsp;

**Explanation:**

&nbsp;


- `(?=.*[0-9])` means that the password must contain a single digit from 1 to 9.  

&nbsp;



- `(?=.*[a-z])` means that the password must contain one lowercase letter. 

&nbsp;


- `(?=.*[A-Z])` means that the password must contain one uppercase letter.  

&nbsp;


- `(?=.*\W)` means that the password must contain one special character.  

&nbsp;

- `.{8,16}` means that the password must be 8-16 characters long. We must use this at the end of the regex, just before the `$` symbol.


&nbsp;

**What are `^` and `$`**:

&nbsp;

`^` indicates the beginning of the string. `$` indicates the end of the string.

If we don't use these `^` & `$`, the regex will not be able to determine the maximum length of the password. In the above example, we have a condition that the password can't be longer than 16 characters, to make that condition work, we have used these `^` & `$`




&nbsp;

**Remove maximum length restriction:**

&nbsp;


- Instead of `.{8,16}`, if we used `.{8,}`, it would mean that the password must be at least 8 characters long. So, there will not be any condition for checking the maximum length of the password.



&nbsp;

**Don't accept any number(digit):**

&nbsp;

- Instead of `(?=.*[0-9])`, if we used `(?!.*[0-9])`, it would mean that the password must not contain any digit from 1-9 (Difference with the `(?=.*[0-9])` is the use of `!` instead of `=`)



&nbsp;

**Don't accept any spcecial character:**

&nbsp;


- Instead of `(?=.*\W)`, if we used `(?!.*\W)`, it would mean that the password must not contain any special characters (The difference with the `(?=.*\W)` is the use of `!` instead of `=`)



&nbsp;

**Alternative Syntax for number(digit):**

&nbsp;


- Instead of `(?=.*[0-9])`, we could have used `(?=.*\d)`.  `(?=.*\d)` also means that the password must contain a single digit from 1 to 9.

&nbsp;








&nbsp;

### Pattern 2:

&nbsp;


> Password must contain one digit from 1 to 9, one lowercase letter, one uppercase letter, one underscore but no other special character, no space and it must be 8-16 characters long.


```js
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*_)(?!.*\W)(?!.* ).{8,16}$/

```


&nbsp;

**Difference with the Pattern 1**

&nbsp;


- Here, we have used `(?=.*_)` which wasn't on the `Pattern 1`.

&nbsp;

- `(?=.*_)(?!.*\W)` means that the password must contain an underscore but can not contain any other special character.



&nbsp;

### Pattern 3:

&nbsp;


> Password must contain one digit from 1 to 9, one lowercase letter, one uppercase letter, one underscore, no space and it must be 8-16 characters long. Usage of any other special character other than underscore is optional.

```js
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*_)(?!.* ).{8,16}$/
```




&nbsp;

**Difference with the Pattern 2**

&nbsp;


- Here, we have not used `(?!.*\W)` what was on the `Pattern 2`.

&nbsp;


- But it still has the `(?=.*_)`

&nbsp;

- By just removing the `(?!.*\W)`, special characters have become optional. Now, one underscore is required but any other special character can be used or not as it's optional. 




&nbsp;

### Pattern 4:

&nbsp;


> Password must contain one digit from 1 to 9, one lowercase letter, one uppercase letter, and one underscore, and it must be 8-16 characters long. Usage of any other special character and usage of space is optional.


```js
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,16}$/
```


&nbsp;

**Difference with the Pattern 3**

&nbsp;



- Here, we have not used  `(?=.*_)` & `(?!.* )` which was on the `Pattern 3`.

&nbsp;


- By removing `(?=.*_)`, it's no longer mandatory to pass one underscore. Now, passing special characters is optional.

&nbsp;

- By removing the `(?!.* )`, usage of space has become optional too. 


&nbsp;

 That's it. 😃 Thanks for Reading.🎉