Password Regex With Min 6 Chars, At Least One Letter and One Number and May Contain Special Characters

javascript regex for password containing at least 8 characters, 1 number, 1 upper and 1 lowercase

Your regular expression should look like:

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

Here is an explanation:

/^
(?=.*\d) // should contain at least one digit
(?=.*[a-z]) // should contain at least one lower case
(?=.*[A-Z]) // should contain at least one upper case
[a-zA-Z0-9]{8,} // should contain at least 8 from the mentioned characters
$/

Regex for Password: "Atleast 1 letter, 1 number, 1 special character and SHOULD NOT start with a special character"

Its simple, just add one more character class at the begining

^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@#$%^&*()_+]{7,19}$
  • [A-Za-z\d] Ensures that the first character is an alphabet or digit.

  • [A-Za-z\d!@#$%^&*()_+]{7,19} will match minimum 7 maximum 19 character. This is required as he presceding character class would consume a single character making the total number of characters in the string as minimum 8 and maximum 20.

  • $ Anchors the regex at the end of the string. Ensures that there is nothing following our valid password

Regex Demo

var pattern = new RegExp(/^(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[A-Za-z\d][A-Za-z\d!@#$%^&*()_+]{7,19}$/);
console.log(pattern.test("!@#123asdf!@#"));
console.log(pattern.test("123asdf!@#"));
console.log(pattern.test("12as#"));

Javascript regular expression password validation having special characters

Use positive lookahead assertions:

var regularExpression = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/;

Without it, your current regex only matches that you have 6 to 16 valid characters, it doesn't validate that it has at least a number, and at least a special character. That's what the lookahead above is for.

  • (?=.*[0-9]) - Assert a string has at least one number;
  • (?=.*[!@#$%^&*]) - Assert a string has at least one special character.

Password regex that requires “at least two of” certain characters

^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)(?![a-z]*$)(?![A-Z]*$)(?![0-9]*$)(?![#?!@$%^&*-]*$).{8,64}$

The string should not contain any symbol outside the 4 groups of characters

^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)

The string should not consist only of lower letters

(?![a-z]*$)

The string should not consist only of upper letters

(?![A-Z]*$)

The string should not consist only of digits

(?![0-9]*$)

The string should not consist only of special characters

(?![#?!@$%^&*-]*$)

The string should consist of 8 to 64 characters

.{8,64}$

UPDATED 2020-09-07

If the string should contain symbols of at list 3 groups of 4

^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)((?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])|(?=.*[a-z])(?=.*[A-Z])(?=.*[#?!@$%^&*-])|(?=.*[a-z])(?=.*[0-9])(?=.*[#?!@$%^&*-])|(?=.*[A-Z])(?=.*[0-9])(?=.*[#?!@$%^&*-])).{8,64}$

The string should not contain any symbol outside the 4 groups of characters

^(?!.*[^A-Za-z0-9#?!@$%^&*-]$)

Then 4 variants of 3 groups of 4 that the symbols should be member of:

(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])

or

(?=.*[a-z])(?=.*[A-Z])(?=.*[#?!@$%^&*-])

or

(?=.*[a-z])(?=.*[0-9])(?=.*[#?!@$%^&*-])

or

(?=.*[A-Z])(?=.*[0-9])(?=.*[#?!@$%^&*-])

and finally the string should consist of 8 to 64 characters

.{8,64}$

RegExp check password 6 chars + 1 number

Keep it simple: if(strPassword.length >= 6 && /\d/.test(strPassword)) will do the work and is way more readable



Related Topics



Leave a reply



Submit