Regex for password with specific restrictions [duplicate] - javascript

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 to check if password is "8 characters including 1 uppercase letter, 1 special character, alphanumeric characters"
(14 answers)
Closed 3 years ago.
I'm trying to come up with a regex that validates passwords. The restrictions are as follows:
Must be at least two of the following:
one lowercase [a-z],
one uppercase [A-Z],
one digit [\d],
one special character [!##\$%\^\&*)(+=._-].
must not begin or end with white-space but can contain white-spaces inside,
must be between 7 and 20 characters long.
So far, this is the last version of what I've come up with:
^(?=.{7,20}$)(?:(?=.*[\d!##\$%\^\&*\)\(+=._-])(?=.*[a-z])\S*|(?=.*[A-Z])(?=.*[\d!##\$%\^\&*\)\(+=._-])\S*|(?=.*[A-Z])(?=.*[a-z])\S*|(?=.*[\d)\(+=._-])(?=.*[!##\$%\^\&*\)\(+=._-])\S*)$
This works for all of the above except letting white-spaces inside. I've gone through multiple regex and this is the best one so far (but also the ugliest).
Edit: Thank you for the fast replies. Why these requirements are in place is beside the point. I know passwords would be more secure if all of the above were required. But as not all customers use password managers...
Now, why is this not a duplicate question? Because no other thread requires any two of the above. They simply start with requiring specific two, than adding another one and so on. This needs to be any two conditions.

Hey you can use below regex to fulfill your requirement
^(?=.\d)(?=.[A-Z])(?=.[a-z])(?=.[^\w\d\s:])([^\s]){7,20}$

Related

Alter Regex to validate email Domain on RFC 5322 from Java to Oracle sql

I need to alter the below Regex to validate emails domains in a oracle database, according to RFC 5322,
and ensuring that the criteria below are respected as well.
Domain rules that must be respected:
must start an end with a letter or digit and be between 1 and 63 characters long.
may contain uppercase and lowercase Latin letters (A to Z and a to z).
may contain digits 0 to 9, provided that top-level domain names are not all-numeric.
may contain hyphen -, provided that it is not the first or last character, and not consecutive also.
must have at least 2 or more characters (abc#t.com is not valid, but abc#tt.com is valid).
I found on the internet the regex below, that works very well and ensure the rules posted above, in Javascript.
My problem is that ORACLE does not support look-ahead/-behind.
#(?:(?=[A-Z0-9-]{1,63}\.)[A-Z0-9]+(?:-[A-Z0-9]+)*\.){1,8}[A-Z]{2,63}$
So, can anyone please help me on making the necessary modifications in order to work in Oracle sql?

email address validation in JavaScript using regular expression [duplicate]

This question already has answers here:
How can I validate an email address in JavaScript?
(79 answers)
Closed 7 years ago.
I'm currently trying to validate email address using regular expression in JavaScript. These are the requirements of the Email address:
The email field contains a user name part follows by "#" and a domain name part.
The user name contains word characters including hyphen ("-") and period (".").
The domain name contains two to four parts of alphabet characters word extension.
Each word extension is separated by a period (".") and the last extension must have two to three characters.
Among four requirements, the third one is most confusing to me. I will be very appreciate if someone can help me. Thank you.
I have tried the first answer in this page, but this answer accept even 5 or more extensions, so it doesn't meet my third requirement.
For Javascript, here is the regex you need which follows the RFC 5322 standard:
/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*#([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i
Side note: it is better to use a very weak regex (basically just matching the '#') and sending a confirmation email.

JavaScript Regex - custom characters and numbers

I am building a RegEx that is almost complete, but I can not get it to check for digits (0 - 9):
So for example: Jones-Parry is valid but Jones-Parry1 is not. The regex at present looks like this:
^([\\w\\s,'\\-ÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜŸäëïöüŸçÇŒœßØøÅåÆæÞþÐð]){0,80}$
I have tried using \d and [0-9] but to no avail. All else is working with the regex aside from the numbers. It validates special characters etc.
Any pointers greatly appreciated!
The problem is \w expands to A-Za-z0-9_, which includes digits 0-9. This explains why strings with digit pass your test.
You may want to specify A-Za-z_ directly instead of \w in your regex. It will fix your problem.
As georg has pointed out in the comment, your regex is very weak, since aside from the length requirement, it only checks whether it does not contain any character outside your allowed character set. A string with only spaces, or a string with only punctuation would pass the test.
Anyway, I doubt validating name is a good idea in general. Many assumptions programmers make about name are wrong. Depending on your requirement, you can give user a field for display name, where user can type anything in, and another field for username, where you only allow a strict set of characters.

Regex for Active Directory Password policies

I need a Regex for Active Directory Password policy with the following conditions.
My requirements are:
at least one digit (0-9)
at least one lowercase character
at least one uppercase character
must contain at least one special character and
the length should be minimum 8 characters and maximum of 25
try this. its included with space bar validation.
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{8,25})

Regex to match multiple patterns in any order

I'm validating a password for complexity in an ASP.NET MVC3 app. My current requirements are that it must contain at least one upper case letter, one lower case letter, one digit and no more than three repeated characters. I'd like to generalise those numbers though, and also add a condition for non-alphanumeric characters.
At present, I'm validating server-side only, so I'm able to call Regex.IsMatch multiple times using one regex for each condition. I want to be able to validate client-side too though. because unobtrusive jQuery validation will only allow one regex, I need to combine all five conditions into a single pattern.
I don't know much when it comes to regular expressions but I've been doing a bit of reading recently. I may be missing something simple but I can't find a way to AND multiple patterns together the way a | will OR them.
You can do this (in .NET) with several lookahead assertions in a single regex:
^(?=.*\p{Lu})(?:.*\p{Ll})(?=.*\d)(?=.*\W)(?!.*(.).*\1.*\1)
will match if all conditions are true.
^ # Match the start of the string
(?=.*\p{Lu}) # True if there is at least one uppercase letter ahead
(?=.*\p{Ll}) # True if there is at least one lowercase letter ahead
(?=.*\d) # True if there is at least one digit ahead
(?=.*\W) # True if there is at least one non-alnum character ahead
(?!.*(.).*\1.*\1) # True if there is no character repeated twice ahead
Note that the match is not going to consume any characters of the string - if you want the match operation to return the string you're matching against, add .* at the end of the regex.
In JavaScript, you can't use Unicode character properties. So instead you could use
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[\W_])(?!.*(.).*\1.*\1)
which will of course only use ASCII letters for validation. If that's OK for you, fine. You could go and augment the character classes like [A-ZÄÖÜÀÈÌÒÙÁÉÍÓÚ] etc. etc. but you would probably never be complete with this. On the server side, if you want the validation to yield the same result, you'd have to specify RegexOptions.ECMAScript so the .NET regex engine behaves like the JavaScript engine (thanks Alan Moore for noticing!).

Categories

Resources