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
How to Count Unique Value from Object of Array in JavaScript
How to Mute Video With JavaScript or Jquery
How to Get the Contextpath from Javascript, the Right Way
Check If a Number Has Repeated Digits
Converting User Input String to Regular Expression
How to Use Owl Carousel Vertically
One Time Page Refresh After First Page Load
How to Stop All Ajax Process in the Page Using Jquery
Force Facebook to Open Link in External Browser for Smart Phones
Uncaught Typeerror: (Intermediate Value)(...) Is Not a Function
Launch Chrome Browser from Internet Explorer
Avoid a Link to Reload the Page in JavaScript
How to Redirect Url After Login Successful Via Ajax Call in PHP
Webcam Js Error in Chrome: Could Not Access Webcam
Stop Chrome Back/Forward Two Finger Swipe
Expand Div on Click With Smooth Animation
How to Download a File With Node.Js (Without Using Third-Party Libraries)
How to Format a Float as Integer When .Tofixed(2) Gives Zeros After the Decimal Point