Regex for Password Strength [duplicate] - javascript

This question already has answers here:
Regex to validate password strength
(11 answers)
Closed 8 years ago.
could someone pleas help me ,
I need an expression for a password for fulfill the following criteria:
at least 8 characters length to a maximum of 15 characters.
at least one letter in upper Case.
at least one letter in lower Case.
at least 1 special character.
at least 1 numeral.
These must be acceptable in any order if possible.
This is an attempt i found but doesn't fulfill the criteria above,i have tried modification but my problem rests in having these in any order and at least one of any of the characters specified ,i have tried reducing each expression below to suit also :
^(?=.*[A-Z].*[A-Z])(?=.*[!##$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
^ Start anchor
(?=.*[A-Z].*[A-Z]) Ensure string has two uppercase letters.
(?=.*[!##$&*]) Ensure string has one special case letter.
(?=.*[0-9].*[0-9]) Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8} Ensure string is of length 8.
$ End anchor.
There is no duplicate ,please review the marking unless your sure it is a duplicate

try this regex will helps you
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W\_])[a-zA-Z0-9\W\_]{8,15}$/

Related

Regex pattern that contains a limited set of special characters [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Closed 3 years ago.
The requirements I've been set are...
MUST match (1 minimum character/number):
1 number (?=.*\d)
1 lower case character (?=.*[a-z])
1 upper case character (?=.*[A-Z])
no whitespace (?!.*\s)
Between 8 and 40 characters .{8,40}
CAN match, but doesn't have to:
Special characters limited to $*%!.,^
This is what I have so far: /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,40}/
I'd like to keep it segmented out the way I do for readability - Unless there's a reason not to?! Happy to change if there are any performance benefits, or if I've done something silly/pointless?
The above works for the most part, including my special characters. However, when I type in a "restricted" character, such as #, it still matches.
I'm a bit lost, so any help would be very much appreciated! Thank you!
Examples of what SHOULD match:
abcABC123
aaBB33!!
!a*Bc9!.abBC*4
Examples of what SHOULD NOT match:
abc ABC 123
abc#ABC?123
áááBBB333
Restrictions:
Anything that is NOT a-z A-Z 0-9 or $*%!.,^ is considered a restricted character
You can use:
^(?=\D*\d)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])[a-zA-Z\d$*%!.,^]{8,40}$
^(?=\D*\d) - require a digit somewhere
(?=[^a-z]*[a-z]) - require a lowercase char somewhere
(?=[^A-Z]*[A-Z]) - require a uppercase char somewhere
[a-zA-Z\d$*%!.,^]{8,40}$ - from start to finish require 8 to 40 of these whitelisted chars in any order
Test your strings one at a time at https://regex101.com/r/lrABwJ/1

Regex Password Validation - Codewars [duplicate]

This question already has answers here:
Regex for password must contain at least eight characters, at least one number and both lower and uppercase letters and special characters
(42 answers)
Regular Expression for alphanumeric with first alphabet and at least one alphabet and one number
(2 answers)
Closed 7 years ago.
Disclaimer: This is a Codewars problem.
You need to write regex that will validate a password to make sure it
meets the following criteria:
At least six characters long
contains a lowercase letter
contains an uppercase letter
contains a number
Valid passwords will only be
alphanumeric characters.
So far, this is my attempt:
function validate(password) {
return /^[A-Za-z0-9]{6,}$/.test(password);
}
What this does so far is make sure that each character is alphanumeric and that the password has at least 6 characters. It seems to work properly in those regards.
I am stuck on the part where it requires that a valid password have at least one lowercase letter, one uppercase letter, and one number. How can I express these requirements along with the previous ones using a single regular expression?
I could easily do this in JavaScript, but I wish to do it through a regular expression alone since this is what the problem is testing.
You need to use lookaheads:
function validate(password) {
return /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[A-Za-z0-9]{6,}$/.test(password);
}
Explanation:
^ # start of input
(?=.*?[A-Z]) # Lookahead to make sure there is at least one upper case letter
(?=.*?[a-z]) # Lookahead to make sure there is at least one lower case letter
(?=.*?[0-9]) # Lookahead to make sure there is at least one number
[A-Za-z0-9]{6,} # Make sure there are at least 6 characters of [A-Za-z0-9]
$ # end of input

Perfect solution to validate a password [duplicate]

This question already has answers here:
regular expression for password with few rules
(3 answers)
Closed 8 years ago.
I took an online JavaScript test where the 3rd problem was as follows:
Complete the checkPassword function, which should check if the password parameter adheres to the following rules:
Must be longer than 6 characters.
Allowed characters are lower or uppercase Latin alphabetic characters (a-z), numbers (0-9), and special characters +, $, #, \, / only.
Must not have 3 or more consecutive numbers (e.g. "pass12p" is fine,
but "pass125p" is not, because it contains "125")
>
My solution was as follows:
function checkPassword(password) {
return /^[a-zA-Z0-9\+\$\\\/#]{6,}$/.test(password) && !/[0-9]{3,}/.test(password);
}
This solution gave me the correct outputs for the given inputs.
But the ultimate test result told that the solution is just 75% correct sadly.
I think the perfect answer is expected to be a solution just with a single regular expression. Is there anyone who can give me a single regular expression that gives the same result? I am not so good at regular expression so please advise.
I appreciate your help in advance.
Just use a negative lookahead at the start.
^(?!.*?\d{3})[a-zA-Z0-9\+\$\\\/#]{6,}$
(?!.*?\d{3}) at the start asserts that the match won't contain atleast three consecutive digits. Then the regex engine tries to match the pattern [a-zA-Z0-9\+\$\\\/#] against the input string 6 or more times only if this condition is satisfied.
DEMO
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d]{6,}$
For consecutive number check
public boolean isValid(final String userName,final String password) {
for(int i=0;(i+2)<userName.length();i++) if(password.indexof(userName.substring(i,i+2))!=-1) return false; return true;
}

Complex password validation using regex

I am trying to create a javascript regular expression for password validation. The rules for a password to get accepted are
Should contain 12 characters or more
Should contain one of these special characters * ^ !
At least two uppercase characters
At least two numbers
At least one lowercase characters
I found an example online and modified it like following
(?=.*[0-9]{2,})(?=.*[a-z])(?=.*[A-Z]{2,}).{12,}
However this still misses the special character requirement and only works if the upper case characters and numbers are in subsequent order. These are the results I got with this one
aMMericano11 - true
aMmeRican1o1 - false
I wanted the second one to be accepted too with the addition of special characters of course.
Can anyone help me on this?
Disregarding my sarcastic comment about the futility of arbitrary password rules, you are trying to do too much at once.
What you're doing is "does it have 12 letters or more and a symbol from *^! and at least two uppercase letters and at least two numbers and at least one lowercase letter"...
What you should do is:
Does it have 12 letters or more? If not, fail and ask for a longer password
Does it have a symbol? If not, fail and ask for a symbol
Does it have at least two uppercase letters? If not, fail and ask for them.
Does it have at least two numbers? If not, fail and ask for them.
Does it have at least one lowercase letter? If not, fail and ask for it.
Break down big problems into small problems, and you'll end up with better user experience because you can tell your user exactly what you want from them.
The problem is in the lookahead:
(?=.*[0-9]{2,})
This pattern requires that the pattern [0-9]{2,} (e.g., a 2-digit number) appear in the text. What you probably intended was:
(?=(.*[0-9]){2,})
This allows the numbers to be separated by other characters, rather than being consecutive.
The same problem applies to the capital letters rule. Piecing this together, the final expression would be:
(?=(.*[0-9]){2,})(?=.*[\*^!])(?=.*[a-z])(?=(.*[A-Z]){2,}).{12,}
The expression will match if and only if the password meets the validity rules. If more granularity is needed (e.g., to detect that a specific rule is violated), you may need to break the expression into smaller expressions.
Agreed with #Niet the Dark Absol
But still if you want to do this with regEx then break it as:
'ab2c3F*ghijKM'.match(/[\w\W]{12,}/) //Should contain 12 characters or more
'ab2c3F*ghijKM'.match(/[*^!]/) //Should contain one of these special characters * ^ !
'ab2c3F*ghijK'.match(/[A-Z]/g).length>=2 //At least two uppercase characters
'ab2c3F*ghijK'.match(/[\d]/g).length>=2 //At least two numbers characters
'23Fa*K'.match(/[a-z]/) //At least one lowercase characters
Then apply && operations on all these expressions
eg:
var pass = 'ab2cFg*hij3KM';
var isValidInput = pass.match(/[\w\W]{12,}/) && pass.match(/[*^!]/) && (pass.match(/[A-Z]/g).length>=2) && (pass.match(/[\d]/g).length>=2) && pass.match(/[a-z]/)
if(isValidInput) {
console.log('valid')
} else {
console.log('invalid')
}

Javascript regex that requires one uppercase letter, one lowercase letter and one number, and allows special [duplicate]

This question already has answers here:
regex to allow atleast one special character, one uppercase, one lowercase(in any order)
(5 answers)
Closed 9 years ago.
I'm trying to make a regex to check a password field, I want to require at least one uppercase letter, one lower case letter and one number.
I also want to allow the user to use special characters, but I don't want special characters to be a requirement.
Is this possible and if so, what regex should I use?
If you don't want special characters to be a requirement, there is no point validating it.
This regex should do: /^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).*$/
Note that this regex will not validate properly for characters not in the default ascii range of a-zA-Z. So if my password was åäöÅÄÖ1234 it would not be valid for this regex, even though there are upper case and lower case letters.
A possible solution, if this is a requirement, would be to rewrite using unicode properties.
Demo: http://regex101.com/r/uO6jB5
From this page:
"Password matching expression. Password must be at least 4 characters, no more than 8 characters, and must include at least one upper case letter, one lower case letter, and one numeric digit."
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$
(note: you'll want to test this to be sure.)

Categories