Expression not working for underscore though included - javascript

Conditions for password
Minimum length should be 7
It must contain at-least one Capital letter
It must contain at-least one numeric
It must contain at-least one special character
/^(?=.{7,50}$)(?=.*[0-9])(?=.*[!##$%^&*_])(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9!##$%^&*_].*/
It is working for passwords Mouse#123 but not working for Mouse_123

Your regex can be simplified to this:
/^(?=.*\d)(?=.*[!##$%^&_])(?=.*[A-Z])[a-zA-Z\d!##$%^&_]{7,50}$/
You need not have (?=.*{7,50}$) lookahead as that can be done outside as well. Besides requirement of at least one small letter is not there in your question.

Related

Password Regex For Angular 4 fails

I have added a regex which will check for least 8 characters, at least one number, one uppercase letter, one lowercase letter and one special character
This is the regex Used
'(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$#$!%*?&])[A-Za-zd$#$!%*?&].{8,}'
The above regex works fine for most of the scenarios but when I use
1oB!gb0s5
or
Pass#123
It fails. Can anyone tell me the issue here.
Here is the portion of your regex which actually consumes the input:
[A-Za-zd$#$!%*?&].{8,}
This means that the password must start with one of the characters in the above character class. It also means that a valid password must have nine or more characters, because the class counts for one, and {8,} means 8 or more. So the following would fail because it does not begin with any such character:
1oB!gb0s5
The second example you gave fails for a different reason, because it only has 8 characters:
Pass#123
I don't know exactly what logic you want here. If you just want to ensure that a password has a lowercase, uppercase, number, and special character, then maybe you can remove the leading character class and just stick with the lookaheads:
(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[$#$!%*?&]).{8,}
Here is a demo which shows that your two example passwords would pass using the above pattern.
Demo

Javascript Regex for strong password with specific ordering of the characters

I have seen answers for strong password checks but I have an additional requirement for the order in which the characters appear.
The password should contain at least:
One upper case letter.
One lower case letter.
One number.
One special character.
The order being:
Should start with upper case and lower case letters.
Followed by number and/or alphabetical characters.
In the end, should be a special character.
e.g.
Xyz1325# is valid.
aBcd123xYz# is also valid.
#Xyz1234 is invalid.
1234Xyz# is invalid.
Xyz# is invalid.
Introducing a specific order of characters in a password makes it relatively more predictable, hence losing the strength of it, and I'll suggest you to get away with that restriction. Nevertheless, you can use this regex that will meet your needs,
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~!##$%^&*])[a-zA-Z][a-zA-Z\d]*[~!##$%^&*?<>]*$
Explanation:
^ --> Start of string
(?=.*[a-z]) --> Ensures the password has at least one lowercase letter.
(?=.*[A-Z]) --> Ensures the password has at least one uppercase letter.
(?=.*\d) --> Ensures the password has at least one digit.
(?=.*[~!##$%^&*]) --> Ensures the password has at least one special character from this set. You can put more characters inside that you want to treat as special.
Now comes the part for ensuring the order. As you said it should start with an alphabet, hence we need the first character as,
[a-zA-Z]
Then following it can be alphabet or numbers hence you can use,
[a-zA-Z\d]*
And finally you want special characters, and by your this statement "In the end, should be a special character." I assume you do not want to restrict it to just one single special character, hence at the end of regex it should be this,
[~!##$%^&*?<>]*
which can match one or more special characters. If you really meant just one special character then just make it [~!##$%^&*?<>]
And finally end it with $ to stop matching the string.
Live Demo
Hope this works for you. Or else, let me know for any other queries.
Edit
Bonus:
If you want to check length as well you can do so using the following:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~!##$%^&*])(?=.{6,18}$)[a-zA-Z][a-zA-Z\d]*[~!##$%^&*?<>]*$
The additional (?=.{6,18}$) is to ensure that your regex has length between 6 to 18.
#Pushpesh,
Please correct if wrong.
The regex I believe you are looking for is this: https://regex101.com/r/nO2DxE/2
Explanation:
These groups (?=.*[A-Z].*) (?=.*[0-9].*) (?=.*[a-z].*) make sure your string contains at least one uppercase letter, one lowercase letter and one digit. The rest of the regex checks that the order you described is respected.
Overall, the regex is:
(?=.*[A-Z].*)(?=.*[0-9].*)(?=.*[a-z].*)^[a-zA-z][a-zA-Z0-9]*[#!#+-]$

Why are my optional characters not being caught?

I'm trying to create a regex to test passwords against. My current one checks for the following:
One Uppercase Letter
One Lowercase Letter
One number
Currently, the user can't enter special characters, however I'm trying to add that as an optional check (so Testing1 and Testing1! should both match). I've tried:
^(?=.*[A-Za-z])(?=.*\d)(?=.*[$#$!%*#?&])(A-Za-z\d[$#$!%*#?&]?){8,}$
But it doesn't catch it. I have a feeling my special character set is in the wrong place, but I'm not sure where to place it.
Where do I add my list of special characters as optional checks?
There's many ways that you can set up your regex, such as creating a whitelist, or a blacklist, for types of characters. This one in particular creates a whitelist for characters that can be used which seems to be what you are looking for.
^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9$#$!%*#?&]{8,}$
Regex Breakdown:
^ // Assert position at start of the line
(?=.*[A-Z]) // First positive lookahead, makes sure a capital character exists
(?=.*[a-z]) // Make sure a lowercase character exists
(?=.*[0-9]) // Make sure a number exists
[A-Za-z0-9$#$!%*#?&] // All of the possible characters that can be typed
{8,} // 8 to infinity characters
$ // Assert position at end of line
Since you say that you want special characters as optional, they are just placed in the possible characters that can be typed, but they are not validated by any positive lookaheads.
See this regex in action on regex101. Keep in mind, the modifiers gm are there to validate across lines in this example and should probably be removed in your use case.
Of course you may have reasons for the "whitelist" approach, but a more common approach, and one you may want to look into trying sometime, is to allow almost anything (blacklist), and then validate that a certain criteria is met.

Limitation of input type in regex

I trying to create a Regular Expression for a password that can accept only
digits and letters (a-zA-Z).
In the password at least one digit must to be, and at least one letter. All the text between 10-12 symbols.
I created the next REGEX :
/^(?=.*[A-Za-z])(?=.*[0-9]).{10,12}$/
The problem is that any signs like $,%me,#,space also acceptable.
How can I add a rule saying any others signs accepts letters,digits are not acatbale?
Instead of using ., which is any character, use [A-Za-z0-9]:
/^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]{10,12}$/
The lookaheads will be satisfied as long as there is a single alphabetic and a single numeric character in the 10-12 list of characters, but . allows any of the other 8-10 characters be anything.
However, I would suggest you not do this because requirements on passwords are not a good thing. You should let people enter whatever password they want.
Try with this:
/^[a-zA-Z0-9]*$/
and with limit:
/^[a-zA-Z0-9]{10,12}$/

Regex password must contain letters , at least one special character and at least one numeric?

I am working on password validation , I am supposed to validate the password as such, it always accept the password only if it contains letters, at least one special character and at least one numeric value.
Can anyone help me with the script for this.
if(myPass.match(/\d/) && myPass.match(/[a-zA-Z]/) && myPass.match(/\W/))
//you're good to go
//edit
the first test sees if there are any digits, the second looks for any and all alphabet characters and the third looks for "non Word" characters. That describes the three classes that you specified. If you wanted to test for a minimum number of chars you would add one more test which would be
&& myPass.length > minimumLength

Categories

Resources