-3

I am trying to create a password regex that:

  • is at least 8 characters in length
  • must start with a capital letter, followed by a number
  • have one of these special characters (#@*)
  • contains exactly 3 numbers at the end of the password

This is what I have tried so far:

/^([A-Z][\d])([#@*]{1})([A-Za-z0-9]+)([\d]{3})$/

The problem with this so far is that it is only allowing a special character to come after the number.

For example: It allows A4#Fgf344 but not A4Fg#f344.

user2538755
  • 314
  • 1
  • 6
  • 21
  • 3
    This has been asked about a hundred times on SO--did you look? Also, please tell your boss/client that these kinds of rules are **really, really** stupid and obnoxious. –  Mar 02 '15 at 19:21
  • possible duplicate of [Regexp Java for password validation](http://stackoverflow.com/questions/3802192/regexp-java-for-password-validation) –  Mar 02 '15 at 19:21
  • 1
    I'm voting to close this question as off-topic because these kinds of password restrictions are dangerous. – Evan Davis Mar 02 '15 at 19:23
  • 1
    I am trying to learn how to use regex properly, it is not for a client or some kind of company, just for my own personal gain – user2538755 Mar 02 '15 at 19:29
  • Understanding you are simply just trying to learn more on regex (which can be tricky, good luck!), I still have to recommend for you to never store password information within a JavaScript file - we can retrieve it easily. If you have a database (MySql for example), start by looking into md5 hashing via PHP. –  Mar 02 '15 at 19:47

2 Answers2

0
^(?=[^#@*]*[#@*][^#@*]*$)([A-Z][\d])([A-Za-z0-9#@*]+)([\d]{3})$

Try this.This should do it.See demo.

https://regex101.com/r/wU7sQ0/50

vks
  • 67,027
  • 10
  • 91
  • 124
  • thank you @vks. This is helpful. I still don't have a clear understanding of look around, but in all this was helpful. – user2538755 Mar 02 '15 at 19:36
0

You can use this regex:

/^[A-Z]\d(?=.*?[#@*]).{3,}\d{3}$/gm

RegEx Demo

anubhava
  • 761,203
  • 64
  • 569
  • 643