regex - minimum of 6 characters including 2 special characters [duplicate] - javascript

This question already has answers here:
Regular expression to match at least two special characters in any order
(3 answers)
javascript regex for special characters
(15 answers)
Closed 3 years ago.
I am not very confortable with regex. I would like the password to have a minimum of 6 characters, including 2 special characters
I tried a first regex but it just returns false each time...
console.log(/^[a-zA-Z0-9!$#%]+$/.test(this.form.password));
I can test the number of characters another way but I would like to include the two special characters through the regex.
Thanks a lot in advance for your help

First lookahead for 6 characters, to ensure that enough exist in the input. Then, repeat twice: 0 or more normal characters, followed by a special character, to ensure that there are at least 2 special characters in the input. Then match 0 or more [special or normal] characters.
/^(?=.{6})(?:[a-z\d]*[!$#%]){2}[a-z\d!$#%]*$/i
https://regex101.com/r/ZyFZPm/2
^ - Start of string
(?=.{6}) - At least 6 characters
(?:[a-z\d]*[!$#%]){2} - Repeat twice:
[a-z\d]* - 0+ alphanumeric characters
[!$#%] - 1 special character
[a-z\d!$#%]* - Match both special and alphanumeric characters up to the end of the string
$ - End of string

Related

Regex pattern that contains a limited set of special characters [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 3 years ago.
The requirements I've been set are...
MUST match (1 minimum character/number):
1 number (?=.*\d)
1 lower case character (?=.*[a-z])
1 upper case character (?=.*[A-Z])
no whitespace (?!.*\s)
Between 8 and 40 characters .{8,40}
CAN match, but doesn't have to:
Special characters limited to $*%!.,^
This is what I have so far: /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).{8,40}/
I'd like to keep it segmented out the way I do for readability - Unless there's a reason not to?! Happy to change if there are any performance benefits, or if I've done something silly/pointless?
The above works for the most part, including my special characters. However, when I type in a "restricted" character, such as #, it still matches.
I'm a bit lost, so any help would be very much appreciated! Thank you!
Examples of what SHOULD match:
abcABC123
aaBB33!!
!a*Bc9!.abBC*4
Examples of what SHOULD NOT match:
abc ABC 123
abc#ABC?123
áááBBB333
Restrictions:
Anything that is NOT a-z A-Z 0-9 or $*%!.,^ is considered a restricted character
You can use:
^(?=\D*\d)(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])[a-zA-Z\d$*%!.,^]{8,40}$
^(?=\D*\d) - require a digit somewhere
(?=[^a-z]*[a-z]) - require a lowercase char somewhere
(?=[^A-Z]*[A-Z]) - require a uppercase char somewhere
[a-zA-Z\d$*%!.,^]{8,40}$ - from start to finish require 8 to 40 of these whitelisted chars in any order
Test your strings one at a time at https://regex101.com/r/lrABwJ/1

How to detect underscore in regex? [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.
I have a regex expression in order to test my password : at least one digit, at least one upper letter,at least one lower letter and at least one special character.
/^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[^\w\s])/
But underscore is not recognized as special character.
How can I improve this ?
The problem here is that, for JavaScript, \w represents:
[a-zA-Z0-9_]
So you are avoiding _ implicitly.
Explaination
This part of your regex:
(?=.*?[^\w\s]) // Match special character
Is equivalent to:
(?=.*?[^a-zA-Z0-9_\s])
Bibliography
The \w metacharacter is used to find a word character.
A word character is a character from a-z, A-Z, 0-9, including the _
(underscore) character.
https://www.w3schools.com/jsref/jsref_regexp_wordchar.asp

Javascript Regex for custom validation [duplicate]

This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
I need regex
1) [A-Z][a-z][0-9] Must include uppercase & lowercase letters, numbers & special characters (except + and -).
2) Not more than 2 identical characters in a sequence (e.g., AAxx1224!# or Password#123 or Google#12 is not acceptable).
I have tried this but dont know how to check 2 identical characters.
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^%*()!&=]).*$
You may add an additional (?!.*(.)\1) lookahead check to disallow consecutive characters and replace .* at the end with [^_+]* (or [^-+]* if you meant hyphens) to match any chars but _ (or -) and +:
^(?!.*(.)\1)(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^*()!&=])[^_+]*$
^^^^^^^^^^ ^^^^^
The (?!.*(.)\1) lookahead matches any 0+ chars other than line breaks chars and then captures these chars one by one and tries to match the identical char immediately after them (with the \1 backreference). If the pattern is found, the whole match is failed.
Note that [^_+] may also match line breaks, but I guess it is not the problem here. Anyway, you can add \n\r there to avoid matching them, too.
See the regex demo

Regex Password Validation - Codewars [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)
Regular Expression for alphanumeric with first alphabet and at least one alphabet and one number
(2 answers)
Closed 7 years ago.
Disclaimer: This is a Codewars problem.
You need to write regex that will validate a password to make sure it
meets the following criteria:
At least six characters long
contains a lowercase letter
contains an uppercase letter
contains a number
Valid passwords will only be
alphanumeric characters.
So far, this is my attempt:
function validate(password) {
return /^[A-Za-z0-9]{6,}$/.test(password);
}
What this does so far is make sure that each character is alphanumeric and that the password has at least 6 characters. It seems to work properly in those regards.
I am stuck on the part where it requires that a valid password have at least one lowercase letter, one uppercase letter, and one number. How can I express these requirements along with the previous ones using a single regular expression?
I could easily do this in JavaScript, but I wish to do it through a regular expression alone since this is what the problem is testing.
You need to use lookaheads:
function validate(password) {
return /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])[A-Za-z0-9]{6,}$/.test(password);
}
Explanation:
^ # start of input
(?=.*?[A-Z]) # Lookahead to make sure there is at least one upper case letter
(?=.*?[a-z]) # Lookahead to make sure there is at least one lower case letter
(?=.*?[0-9]) # Lookahead to make sure there is at least one number
[A-Za-z0-9]{6,} # Make sure there are at least 6 characters of [A-Za-z0-9]
$ # end of input

Regular Expression to validate Alphanumeric value in JavaScript [duplicate]

This question already has answers here:
alphanumeric regex javascript
(3 answers)
Closed 9 years ago.
I want to check if a string is STRICTLY ALPHANUMERIC in javascript. A valid string should not contain any special characters and must have atleast one uppercase or lowercase alphabet and a digit.
aaa, 1111, 112##, 12qw# : Invalid
aaa111, 12abc, abc123abc, 12bc12 : valid
Can anyone help me to frame my regular expression?
Using /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i:
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('111aa')
true
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('aaa222')
true
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('aaa')
false
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('222')
false
> /^(\d+[a-z]|[a-z]+\d)[a-z\d]*$/i.test('112aaa#')
false
/^[a-z0-9]*([a-z]\d|\d[a-z])[a-z0-9]*$/i
The import bit is in the middle: it's either a letter followed by a digit, or a digit followed by a letter. If a valid string contains at least one of each, there must be part of the string where this is true. Then we just pad on both sides with zero or more alphanumeric characters.
See http://regex101.com/r/wZ0qL2

Categories