Regex for Active Directory password [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)
Closed 5 years ago.
Been trying all morning to figure out a regex pattern for an AD password restriction we're trying to enforce. Any ideas?
MUST have at least one lower case character ( a-z )
MUST have at least one upper case character ( A-Z )
MUST have at least one numerical character ( 0-9 )
MUST have at least one of the following special characters, but must be able to permit all:
! # # $ % ^ & * ( ) - _ + = { } [ ] | \ : ; " ' < > , . ? /
8 to 14 characters long
Can be in ANY order
I've tried about 50 combinations and the special characters part eludes me.
The one's I've found on here or online don't include the bracket special characters and a few others unfortunately.

Multiple seperate lookaheads from the start of string should work (demo)
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[#!#$%^&*()\-_+={}[\]|\\:;"'<>,.?\/]).{8,14}$
^ # anchors to start of string
(?=.*?[a-z]) # lookahead for lowercase
(?=.*?[A-Z]) # lookahead for uppercase
(?=.*?[0-9]) # lookahead for numbers
(?=.*?[#!#$%^&*()\-_+={}[\\]|\:;"'<>,.?\/]) # lookahead for special characters
.{8,14} # the actual capture, also sets boundaries for 8-14
$ # anchors to end of string
Updated to include !, and #. Missed them in first test.
Updated to escape hyphen.

Related

Regex for validating username [duplicate]

This question already has answers here:
Regular expression to validate username
(12 answers)
Closed 2 years ago.
I have tried lots of regex patterns but I couldn't find a perfect regex which does the following (min. 3 char and max. 30, special characters can be allowed, if all characters are only special characters it should return false).
Expectation:
"ben":true,(3 char satisfied)
"ab":false, (3 char not satisfied)
"ben franklin":true, (space can be allowed)
"ben_franklin":true, (special char can be allowed)
"ben#:true, (special char and min 3 char satisfied)
"%#$":false, (though 3char ,but its all special char)
"ben#franklin":true,
"be'franklin":true
"&%^$##!*&^":false
If by "special characters", you mean any non-alphanumeric characters, you may use the following pattern:
^(?=.*[A-Za-z0-9]).{3,30}$
Demo.
Breakdown:
^ # Beginning of the string.
(?=.*[A-Za-z0-9]) # Must contain at least one alphanumeric (English) character.
.{3,30} # Match any character (special or not) between 3 and 30 times.
$ # End of string.
Further adjustments:
If you want to add more characters to the "not special chars", you may add them inside the character class. For example, to add the space char, you may use [A-Za-z0-9 ].
If you want to limit the "special characters" to a particular set of characters, then you may replace .{3,30} with [A-Za-z0-9##$%....]{3,30}.
I would probably just do this with 2 passes:
let str = "ben#franklin";
let result = !!(str.match(/^[a-z\s!##$%^&_-]{3,30}$/g) && str.match(/^.*[a-z].*$/g));
console.log(result);
Check for valid characters.
Make sure at least one a-z character.
You didn't mention numbers or uppercase characters, so I didn't add them.

Password policy requirement regex

I need a regex to meet this password policy requirement
Minimum eight (8) characters
At least one number (0-9)
Any three of the following:
Lowercase
Uppercase
Number
Special character ( ! " # $ % & ' ( )
* + , - . / : ; < = > ? # [ \ ] ^ _ ` { | } ~ )
for now i am using this regex Minimum eight (8) characters
/^(?=.*\d)[ !#$%&()*+,.\/:;<=>?#[\]^`{|}~\w-]{8,}$/
This regex is not working as expected it is taking input of
testtest1 as a right match.It should take this test#test1,Testtest12 as a right input
The pattern will match testtest1 as you are only asserting a required digit and following character class [ !#$%&()*+,.\/:;<=>?#[\]^{|}~\w-]{8,}` will repeat matching at least 8 times any of the listed.
If you want to assert either an uppercase char A-Z or a special character as well, you could use another positive lookahead with an alternation
^(?=.*\d)(?=.*(?:[A-Z]|[!#$%&()*+,.\/:;<=>?#[\]^`{|}~-]))[!#$%&()*+,.\/:;<=>?#[\]^`{|}~\w-]{8,}$
Regex demo
Note that I omitted matching the spaces, you could a space to the character class if you want to match it (Not sure if you would allow spaces in the password)
You need to change your lookahead part a bit.
^(?=.{8,})(?=.*[0-9].*)(.*[\!"#$%&'\(\)\*+,\-.\/:;<=>?#\[\\\]^_`{|}~].*){3,}
(?=.{8,}) = minimum eight characters length (positive lookahead)
(?=.*[0-9].*) = at leats one number (positive lookahead)
(.*[\!"#$%&'\(\)\*+,\-.\/:;<=>?#\[\\\]^_{|}~].*){3,}` any character in your list somewhere at least three times

JS regex to match a username with specific special characters and no consecutive spaces

I am pretty new to this reg ex world. Struck up with small task regarding Regex.
Before posting new question I have gone thru some answers which am able to understand but couldnt crack the solution for my problem
Appreciate your help on this.
My Scenario is:
Validating the Username base on below criteria
1- First character has to be a-zA-Z0-9_# (either of two special characters(_#) or alphanumeric)
2 - The rest can be any letters, any numbers and -#_ (either of three special characters and alphanumeric).
3 - BUT no consecutive spaces between words.
4- Max size should be 30 characters
my username might contain multiple words seperated by single space..for the first word only _# alphanumeric are allowed and for the second word onwards it can contain _-#aphanumeric
Need to ignore the Trailing spaces at the end of the username
Examples are: #test, _test, #test123, 123#, test_-#, test -test1, #test -_#test etc...
Appreciate your help on this..
Thanks
Arjun
Here you go:
^(?!.*[ ]{2,})[\w#][-#\w]{0,29}$
See it working on regex101.com.
Condition 3 is ambigouus though as you're not allowing spaces anyway. \w is a shortcut for [a-zA-Z_], (?!...) is called a neg. lookahead.
Broken down this says:
^ # start of string
(?!.*[ ]{2,}) # neg. lookahead, no consecutive spaces
[\w#] # condition 1
[-#\w]{0,29} # condition 2 and 4
$ # end of string
This might work ^(?=.{1,30}$)(?!.*[ ]{2})[a-zA-Z0-9_#]+(?:[ ][a-zA-Z0-9_#-]+)*$
Note - the check for no consecutive spaces (?! .* [ ]{2} ) is not really
necessary since the regex body only allows a single space between words.
It is left in for posterity, take it out if you want.
Explained
^ # BOS
(?= .{1,30} $ ) # Min 1 character, max 30
(?! .* [ ]{2} ) # No consecutive spaces (not really necessary here)
[a-zA-Z0-9_#]+ # First word only
(?: # Optional other words
[ ]
[a-zA-Z0-9_#-]+
)*
$ # EOS

Need to build regex to validate password field [duplicate]

can any one help me in creating a regular expression for password validation.
The Condition is "Password must contain 8 characters and at least one number, one letter and one unique character such as !#$%&? "
^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$
---
^.* : Start
(?=.{8,}) : Length
(?=.*[a-zA-Z]) : Letters
(?=.*\d) : Digits
(?=.*[!#$%&? "]) : Special characters
.*$ : End
Try this
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,20})
Description of above Regular Expression:
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[\W]) # must contains at least one special character
. # match anything with previous condition checking
{8,20} # length at least 8 characters and maximum of 20
) # End of group
"/W" will increase the range of characters that can be used for password and pit can be more safe.
You can achieve each of the individual requirements easily enough (e.g. minimum 8 characters: .{8,} will match 8 or more characters).
To combine them you can use "positive lookahead" to apply multiple sub-expressions to the same content. Something like (?=.*\d.*).{8,} to match one (or more) digits with lookahead, and 8 or more characters.
So:
(?=.*\d.*)(?=.*[a-zA-Z].*)(?=.*[!#\$%&\?].*).{8,}
Remembering to escape meta-characters.
Password with the following conditions:
At least 1 digit
At least 2 special characters
At least 1 alphabetic character
No blank space
'use strict';
(function() {
var foo = '3g^g$';
console.log(/^(?=.*\d)(?=(.*\W){2})(?=.*[a-zA-Z])(?!.*\s).{1,15}$/.test(foo));
/**
* (?=.*\d) should contain at least 1 digit
* (?=(.*\W){2}) should contain at least 2 special characters
* (?=.*[a-zA-Z]) should contain at least 1 alphabetic character
* (?!.*\s) should not contain any blank space
*/
})();
You can make your own regular expression for javascript validations;
(/^
(?=.*\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
$/)
Example:- /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/
var regularExpression = new RegExp("^^(?=.*[A-Z]{"+minUpperCase+",})" +
"(?=.*[a-z]{"+minLowerCase+",})(?=.*[0-9]{"+minNumerics+",})" +
"(?=.*[!##$\-_?.:{]{"+minSpecialChars+",})" +
"[a-zA-Z0-9!##$\-_?.:{]{"+minLength+","+maxLength+"}$");
if (pswd.match(regularExpression)) {
//Success
}

Regular Expression for password validation

can any one help me in creating a regular expression for password validation.
The Condition is "Password must contain 8 characters and at least one number, one letter and one unique character such as !#$%&? "
^.*(?=.{8,})(?=.*[a-zA-Z])(?=.*\d)(?=.*[!#$%&? "]).*$
---
^.* : Start
(?=.{8,}) : Length
(?=.*[a-zA-Z]) : Letters
(?=.*\d) : Digits
(?=.*[!#$%&? "]) : Special characters
.*$ : End
Try this
((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,20})
Description of above Regular Expression:
( # Start of group
(?=.*\d) # must contains one digit from 0-9
(?=.*[a-z]) # must contains one lowercase characters
(?=.*[\W]) # must contains at least one special character
. # match anything with previous condition checking
{8,20} # length at least 8 characters and maximum of 20
) # End of group
"/W" will increase the range of characters that can be used for password and pit can be more safe.
You can achieve each of the individual requirements easily enough (e.g. minimum 8 characters: .{8,} will match 8 or more characters).
To combine them you can use "positive lookahead" to apply multiple sub-expressions to the same content. Something like (?=.*\d.*).{8,} to match one (or more) digits with lookahead, and 8 or more characters.
So:
(?=.*\d.*)(?=.*[a-zA-Z].*)(?=.*[!#\$%&\?].*).{8,}
Remembering to escape meta-characters.
Password with the following conditions:
At least 1 digit
At least 2 special characters
At least 1 alphabetic character
No blank space
'use strict';
(function() {
var foo = '3g^g$';
console.log(/^(?=.*\d)(?=(.*\W){2})(?=.*[a-zA-Z])(?!.*\s).{1,15}$/.test(foo));
/**
* (?=.*\d) should contain at least 1 digit
* (?=(.*\W){2}) should contain at least 2 special characters
* (?=.*[a-zA-Z]) should contain at least 1 alphabetic character
* (?!.*\s) should not contain any blank space
*/
})();
You can make your own regular expression for javascript validations;
(/^
(?=.*\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
$/)
Example:- /^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/
var regularExpression = new RegExp("^^(?=.*[A-Z]{"+minUpperCase+",})" +
"(?=.*[a-z]{"+minLowerCase+",})(?=.*[0-9]{"+minNumerics+",})" +
"(?=.*[!##$\-_?.:{]{"+minSpecialChars+",})" +
"[a-zA-Z0-9!##$\-_?.:{]{"+minLength+","+maxLength+"}$");
if (pswd.match(regularExpression)) {
//Success
}

Categories

Resources