Password policy requirement regex - javascript

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

Related

Email fixed validation with regular expression in Javascript

I am working on email validations using regular expressions, where I need some fixed email values to validate before proceeding
Here is a clear requirement
User can only have up to 50 character before the Octets (#) and a maximum of 25 characters after the Octets, with a total of 80 characters
I used normal regex to validate email like
let reg = /^\w+([\.-]?\w+)*#\w+([\.-]?\w+)*(\.\w{2,3})+$/;
And now for the above requirement I am trying to create an expression like
let reg = /^\w+([\.-]?(\w{2,50})+)*#(\w{2,25})+([\.-]?\w+)*(\.\w{2,3})+$/;
But it's taking only starting character(not accepting lower than two) and not the last number of requirement, So any help would be appreciated.
If you want to match 1-50 word chars before the # (so no . or -) and 1-25 word chars after the # (also no . or -) and a total of no more than 80 chars:
^(?!.{81})(?:\w+[.-])*\w{1,50}#\w{1,25}(?:[.-]\w+)*\.\w{2,3}$
Explanation
^ Start of string
(?!.{81}) Negative lookahead, assert that from the current position there are not 81 chars directly to the right
(?:\w+[.-])* Repeat 0+ times 1+ word chars and either a . or -
\w{1,50} Match 1-50 word chars
# Match literally
\w{1,25} Match 1-25 word chars
(?:[.-]\w+)* Repeat 0+ times matching either a . or - and 1+ word chars
\.\w{2,3} Match a . and 2-3 word chars
$ End of string
Regex demo

Regex for validating statement descriptor for credit card statements

I'm trying to validate a string entered by the user to be used as the statement description on the credit card statement to describe the purchase.
The requirements are:
Must be between 5 and 22 characters long
Must contain at least one letter (case doesn't matter)
Cannot contain these characters: < > \ ' "
Only ASCII characters allowed
Here's what I've got so far, which is kind of working:
/^(?=.*?[a-zA-Z])[a-zA-Z0-9]{5,22}$/gm
...in that it correctly checks the length for 5-22 characters long and checks for at least one letter. However, it disallows all special characters and diacritics instead of just the few that aren't allowed. How do I modify it to allow the other allowed characters?
You could use a positive lookahead to assert a character and a negative lookahead to assert not to match any character listed in the character class.
For Javascript you can use the case insensitive flag /i and use [a-z].
Edit: As Wiktor Stribiżew points out, to match only ASCII characters you could use [\x00-\x7F] instead of using a dot.
^(?=.*[a-z])(?!.*[<>\\'"])[\x00-\x7F]{5,22}$
^ Start of string
(?=.*[a-z]) Positive lookahead to check if there is a ASCII letter
(?!.*[<>\\'"]) Negative lookahead to check that there is not any of the chars in the character class
[\x00-\x7F]{5,22} Match any ASCII character 5 - 22 times
$ End of the string
For example:
const regex = /^(?=.*[a-z])(?!.*[<>\\'"])[\x00-\x7F]{5,22}$/gmi;
See the regex demo
You may use
/^(?=[^a-z]*[a-z])(?:(?![<>\\'"])[\x00-\x7F]){5,22}$/i
/^(?=[^a-z]*[a-z])(?![^<>\\'"]*[<>\\'"])[\x00-\x7F]{5,22}$/i
If you mean printable ASCII chars are allowed use
/^(?=[^a-z]*[a-z])(?:(?![<>\\'"])[ -~]){5,22}$/i
/^(?=[^a-z]*[a-z])(?![^<>\\'"]*[<>\\'"])[ -~]{5,22}$/i
Details
^ - start of string
(?=[^a-z]*[a-z]) - there must be at least 1 ASCII letter in the string
(?:(?![<>\\'"])[ -~]){5,22} - five to twenty-two occurrences of any printable ASCII char other than <, >, \, ' and " (if [\x00-\x7F] is used, any ASCII char other than the chars in the negated character class)
(?![^<>\\'"]*[<>\\'"]) - no <, >, \, ' and " allowed in the string
$ - end of string.

Regex for Active Directory password [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 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.

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