Number or special character requirement addition to password validation regex - javascript

I need to validate password with: At least one uppercase, at least one lowercase, at least one number OR symbol, at least 8 characters.
I have this regex:
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9]).{8,20}$/
This works fine except of > it checks string on number AND symbol, but not on number OR symbol. And also character length 8-20, not at least 8 but gives range. I want it to check number OR symbol. Any ideas? Thanks and have a good day!

The (?=.*\d) positive lookahead requires a digit in the string AND (?=.*[^a-zA-Z0-9]) requires a char other than ASCII letter or digit.
To make the regex require a digit OR a char other than ASCII letter or digit, merge the two lookaheads as
(?=.*[^A-Za-z])
Basically, you need to remove 0-9 from the second lookahead and it will require any char but an ASCII letter.
Result:
/^(?=.*[^A-Za-z])(?=.*[a-z])(?=.*[A-Z]).{8,20}$/
Or, a much more efficient version based on the contrast principle:
/^(?=[A-Za-z]*[^A-Za-z])(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z]).{8,20}$/
See the regex demo.
If a space is not special, add it to the lookahead:
/^(?=[A-Za-z ]*[^A-Za-z ])(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z]).{8,20}$/
^ ^

Related

Match the alphanumeric words(NOT NUMERIC-ONLY words) which have unique digits

Using regular expression, I want to select only the words which:
are alphanumeric
do not contain only numbers
do not contain only alphabets
have unique numbers(1 or more)
I am not really good with the regex but so far, I have tried [^\d\s]*(\d+)(?!.*\1) which takes me nowhere close to the desired output :(
Here are the input strings:
I would like abc123 to match but not 123.
ab12s should also match
Only number-words like 1234 should not match
Words containing same numbers like ab22s should not match
234 should not match
hel1lo2haha3hoho4
hel1lo2haha3hoho3
Expected Matches:
abc123
ab12s
hel1lo2haha3hoho4
You can use
\b(?=\d*[a-z])(?=[a-z]*\d)(?:[a-z]|(\d)(?!\w*\1))+\b
https://regex101.com/r/TimjdW/3
Anchor the start and end of the pattern at word boundaries with \b, then:
(?=\d*[a-z]) - Lookahead for an alphabetical character somewhere in the word
(?=[a-z]*\d) - Lookahead for a digit somewhere in the word
(?:[a-z]|(\d)(?!\w*\1))+ Repeatedly match either:
[a-z] - Any alphabetical character, or
(\d)(?!\w*\1) - A digit which does not occur again in the same word
Here is a bit shorter & faster regex to make it happen since it doesn't assert negative lookahead for each character:
/\b(?=[a-z]*\d)(?=\d*[a-z])(?!\w*(\d)\w*\1)[a-z\d]+\b/ig
RegEx Demo
RegEx Details:
\b: Word boundary
(?=[a-z]*\d): Make sure we have at least a digit
(?=\d*[a-z]): Make sure we have at least a letter
(?!\w*(\d)\w*\1): Make sure digits are not repeated anywhere in the word
[a-z\d]+: Match 1+ alphanumericals
\b: Word boundary
You could assert all the conditions using one negative lookahead:
\b(?![a-z]+\b|\d+\b|\w*(\d)\w*\1)[a-z\d]+\b
See live demo here
The important parts are starting match from \b and immediately looking for the conditions:
[a-z]+\b Only alphabetic
\d+\b Only numeric
\w*(\d)\w*\1 Has a repeating digit
You can use this
\b(?!\w*(\d)\w*\1)(?=(?:[a-z]+\d+)|(?:\d+[a-z]+))[a-z0-9]+\b
\b - Word boundary.
(?!\w*(\d)\w*\1) - Condition to check unique digits.
(?=(?:[a-z]+\d+)|(?:\d+[a-z]+)) - Condition to check alphanumeric words.
[a-z0-9]+ - Matches a to z and 0 to 9
Demo

Regex that starts with letter, contains one uppercase/one lowercase letter, one number and no special characters & min 8 characters

I am trying to write a regex that:
starts with a letter
contains one uppercase and one lowercase letter
contains one number
does not allow special characters
minimum 8 characters
So far I have the upper/lowercase conditions, the number and minimum character requirements set with the following regex:
/^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).{8,}$/
My best guess at resolving the starts with a letter and does not allow special characters requirements are below. This regex seems to evaluate all input to false:
/^[a-zA-Z](?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9]).{8,}$/
You need to put the lookaheads after ^ and put [a-zA-Z] right after them and quantify the rest with {7,}:
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])[a-zA-Z][a-zA-Z0-9]{7,}$
See the regex demo.
Pattern details:
^ - start of a string
(?=.*?[a-z]) - at least 1 lowercase ASCII letter
(?=.*?[A-Z]) - at least 1 uppercase ASCII letter
(?=.*?[0-9]) - at least 1 ASCII digit
[a-zA-Z] - an ASCII letter
[a-zA-Z0-9]{7,} - 7 or more ASCII letters or digits (\w also allows _)
$ - end of string.

JavaScript Regular Expression OR Operator

Saw a challenge on Twitter so I've been working my way through it, granted I am not the best with Regular Expressions. This is what I have so far:
var pass_regex = new RegExp(/^[a-z][A-Z][0-9]|[!##$%^&*()_]+$/);
I am trying to match a password input that contains:
1 Lowercase Letter
1 Uppercase Letter
1 Digit OR Special Character
Where I am getting stuck is on the 'OR' part, I thought the pipe separator between [0-9] and my set of special characters would work but it doesn't seem to. Trying to better understand how you would use regular expressions to to check for 1 Digit OR 1 Special Character. Thank you in advance for any help provided.
Atleast one:
You need to use a positive lookahead based regex for checking multiple conditions.
^(?=.*?[A-Z])(?=.*?[a-z]).*?[\W\d].*
OR
^(?=.*?[A-Z])(?=.*?[a-z]).*?[!##$%^&*()_\d].*
(?=.*?[A-Z]) Asserts that there must be atleast one uppercase letter.
(?=.*?[a-z]) Atleast one lowercase letter.
.*? non-greedy match of any character zero or more times.
If the above conditions are satisfied then match that corresponding string and also the string must contain atleast a single character from the given list [!##$%^&*()_\d] . \d in this list matches any digit character.
.* matches the following zero or more characters.
DEMO

Javascript regular expression

I am making form and there is only one more thing which I cant figure it out :(
I need regular expression for password which must be at least 7 characters long. There can be small and big letters and must contain at least one number.
I tried
[0-9]+[a-zA-Z]){7}$
You can use lookahead:
^(?=.*\d)[a-zA-Z\d]{7,}$
(?=.*\d) is a lookahead which checks for a digit in the string. Basically, .* matches the whole string and then backtracks 1 by 1 to match a digit. If it matches a digit, the regex engine comes back to its position before match. So, it just checks for a pattern.
{7,} is a quantifier which matches previous pattern 7 to many times
^ is the beginning of a string

How to write Regular expression for minimum one character in javascript?

I have small requirement in Regular expression,here I need minimum of one letter of Alphabets and followed by numbers and special characters. I tried the following regular expressions but I'm not getting the solution.
/^[a-zA-Z0-9\-\_\/\s,.]+$/
and
/^([a-zA-Z0-9]+)$/
I need minimum of one letter of Alphabets
[a-z]+
and followed by numbers and special characters.
[0-9_\/\s,.-]+
Combined together you would get this:
/^[a-z]+[0-9_\/\s,.-]+$/i
The /i modifier is added for case insensitive matching of alphabetical characters.
Try this regex:
/^[a-z][\d_\s,.]+$/i
To clarify what this does:
^[a-z] // must start with a letter (only one) add '+' for "at least one"
[\d_\s,.]+$ // followed by at least one number, underscore, space, comma or dot.
/i // case-insensitive
You need the other character selection to be separate. I'm confused as to what "numbers and special characters" means, but try:
/^[a-z]+[^a-z]+$/i

Categories