JavaScript password validation regex failing on order of groupings - javascript

My password validation criteria is as follows:
Must contain at least two lower case characters
Must contain at least two upper case characters
Must contain at least two numeric characters
Must contain at least two special characters i.e. ##$%
Must be at least 11 characters long
I tried using this for the first four criteria:
/(?:\d{2,})(?:[a-z]{2,})(?:[A-Z]{2,})(?:[!"'#$&()*+-#,.:;<>=?^_`{|}~\/\\]{2,})/g
But it does not match the following string which i would expect it to:
12QAqa##
But it does match:
12qaQA##
The order that the validation criteria is not important. How do i rewrite the regex to not take into account the order?

The following seems to meet all your requirements:
/*
Must contain at least two lower case characters
Must contain at least two upper case characters
Must contain at least two numeric characters
Must contain at least two special characters i.e. ##$%
Must be at least 11 characters long
*/
var password ='12qaQ##123456789A';
var pattern =/^(?=(.*[a-z]){2,})(?=(.*[A-Z]){2,})(?=(.*[0-9]){2,})(?=(.*[!##\$%]){2,}).{11,}$/;
alert( pattern.test(password) );
https://jsfiddle.net/rryg67v1/1/
^ // start of line
(?=(.*[a-z]){2,}) // look ahead and make sure at least two lower case characters exist
(?=(.*[A-Z]){2,}) // look ahead and make sure at least two upper case characters exist
(?=(.*[0-9]){2,}) // look ahead and make sure at least two numbers exist
(?=(.*[!##\$%]){2,}) // look ahead and make sure at least two special characters exist
.{11,} // match at least 11 characters
$ // end of line
Good luck!!

Related

How to enable adding special character to the password validation in regular expression

I am using this regular expression to validate password entry but this is not accepting any special character
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z]{8,}$/
with this 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
$/
How can I add the following to the expression as well?
/[!##$%\^&*(){}[\]<>?/|\-]/
Just add another positive-lookahead group if you want to ensure that there is a special character, and then add the special characters to the matching group:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!##$%\^&*(){}[\]<>?/|\-])[0-9a-zA-Z!##$%\^&*(){}[\]<>?/|\-]{8,}$/

regex password validation angularjs

I am new to angular js. I have created a login screen. I need to validate my password. it should contain one special character from -'$#£!%*#?&' and at least one letter and number. As of now it accepts all special characters without any limitations. I have following code
if (vm.newpassword_details.password.search("^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*?[$#£!%*#?&]).{8,}$")) {
var msg = "Password should contain one special character from -'$#£!%*#?&' and at least one letter and number";
alert(msg);
}
Note that your current regex imposes 4 types of restriction:
At least one ASCII letter ((?=.*?[A-Za-z])),
At least one digit ((?=.*?[0-9])),
At least one specific char from the set ((?=.*?[$#£!%*#?&]))
The whole string should have at least 8 chars (.{8,})
The . in .{8,} can match any char other than line break chars.
If you plan to restrict the . and only allow users to type the chars from your sets, create a superset from them and use it with RegExp#test:
if (!/^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*?[$#£!%*#?&])[A-Za-z0-9$#£!%*#?&]{8,}$/.test(vm.newpassword_details.password)) { /* Error ! */ }
See the regex demo

Regex to allow any language characters in the form of full name and starting with letter

I try to validate a name field, and for this field I like to allow the end user to add anything like Merianos Nikos, Μέριανος Νίκος (greek), or characters from any other language in the same form.
The form is first letter capital, rest letters of the word lower, and at least two words.
Currectly I have this regex /^([A-Z][a-z]*((\s)))+[A-Z][a-z]*$/ that works perfectly with english, but not with greeks and perhaps with other languages.
Finally, I'd like to validate another field with at least on word, with the frist letter capital, but this field can also contains characters after the word.
For the moment I use the followign regex /^[\s\w\.\-_]+$/ that works, but again I have problem with greek and other languages.
You could do this through the use of Unicode Categories. Thus, the regular expression ^\p{Lu}\p{Ll}+( \p{Lu}\p{Ll}+)*$ will match an upper case letter followed by one or more lower case letters, followed by 0 or more other names, seperated by spaces. An example of the expression is shown here.
With regards to your second point, you could use something of the sort ^\p{Lu}\p{Ll}*$, which will expect at least 1 upper case letter followed by 0 or more lower case ones.
The expressions above assume that you do not have quotation marks, such as O'Brian or dashes Foo-bar in your names. If you want to handle specifically Greek names, and you know for a fact that Greek names have neither quotation marks nor dashes in them, then this should not be much of a problem.
Usually one simply ensures that the name provided is not empty, rather than specifying some strict form. Please refer to this question for more information on the matter.
^[{\p{L}}{0-9}]$
This regex matches any kind of letter from any language (and also numbers).
function isFullname($fullname) {
return preg_match("/^((?:\p{Ll}|\p{Lu}){2,30}\s?){2,4}$/g", $fullname);
}
This is useful for me. Because the username may also be written in lowercase letters.
And it can have a name or surname of at least 2 characters. Also, I accept a name with a maximum of 30 characters. And I make it repeatable at least 2 times at most 4 times.
It could have a name like McCésy (realy? =)) ...

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')
}

Regular expression for minimum of 10 characters and include 1 numeral and one capital letter

I want to validate a string using jQuery.match() function. String must contain
Minimum of 10 characters.
Must contains atleast one numeral.
Must contain atleast one capital letter.
How can I do that? Can anyone show me the regular expression to do that?
I am already have this:
^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^&+=]).*$
but it is validating like this only, allow atleast one special character, one uppercase, one lowercase(in any order).
It is not strictly conforming to the length restriction, because you haven't done it correctly. The first look-ahead - (?=.{8,}), is just testing for string with minimum length 8. Remember, since the look-arounds are 0-length assertions, the look-aheads after .{8,0} are not consuming any character at all.
In fact, you can remove that first look-ahead, and simply use that quantifier at the end while matching.
Try this regex:
^(?=.*[A-Z])(?=.*[0-9]).{10,}$
Break up:
^
(?=.*[A-Z]) # At least an uppercase alphabet
(?=.*[0-9]) # At least a numeral
.{10,} # Any character 10 or more times
$
I'm not sure how you got that regex; it seems to have been taken somewhere...
^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^&+=]).*$
^^^^ ^^^^^ ^^^^^ ^--------^
1 2 3 4
Makes sure there's at least 8 characters
Makes sure there's lowercase characters
Makes sure there's uppercase characters
Makes sure there are those special characters.
To make a regex to your requirements, do some changes:
^(?=.{10})(?=.*[0-9])(?=.*[A-Z]).*$
^^^^ ^^^^^ ^^^^^
1 2 3
Makes sure there's at least 10 characters
Makes sure there's at least a number.
Makes sure there's at least an uppercase letter.
You can make it a bit shorter using:
^(?=.*[0-9])(?=.*[A-Z]).{10,}$
^ # Start of group
(?=.*\d) # must contain at least one digit
(?=.*[A-Z]) # must contain at least one uppercase character
. # match anything with previous condition checking
{10,} # length at least 10 characters
$ # End of group
i.e.:
^(?=.*\d)(?=.*[A-Z]).{10,}$
Source:
Password matching expression

Categories