I need the password to fulfill these requirements
Password must contain at least 8 word characters
Must have at least 1 numeric digit e.g. 3
Must have at least 2 uppercase characters but not in one consecutive sequence
It doesn't seems to work with this
var pos = myPass.value.search(/^([\w.-]{8,})(?=.*\d)((.*?[A-Z]){2,})$/);
No.3 is the hardest.
You are mixing consuming and non-consuming patterns adding limiting quantifiers to the consuming patterns that match a sequence, while you need to just check if a string matches some restrictive patterns or not. To add those restrictions you need lookaheads. (?=.*\d) is a correct part of your regex, other aren't.
Also, a RegExp#test() is a better method to check if a string matches or not.
Use
/^(?=\D*\d)(?=(?:(?:^|[^A-Z]+)[A-Z]){2}).{8,}$/.test(myPass.value)
See the regex demo
Or, to allow only letters, digits, underscores, dots and hyphens in the password:
/^(?=\D*\d)(?=(?:(?:^|[^A-Z]+)[A-Z]){2})[\w.-]{8,}$/.test(myPass.value)
^^^^^^
Details:
^ - start of string
(?=\D*\d) - after 0+ non-digits (\D*) at the start of the string, there must be a digit (\d) (note that after this lookahead execution, the regex index is still at the beginning of the string)
(?=(?:(?:^|[^A-Z]+)[A-Z]){2}) - there must be 2 sequences ((?:...){2}) of:
(?:^|[^A-Z]+) - start of string or one or more chars other than uppercase letters
[A-Z] - an uppercase letter.
.{8,} - any 8 or more chars other than those used in a linebreak sequence
OR
[\w.-]{8,} - 8 or more ASCII letters and digits, underscores, dots or hyphens
$ - end of string.
Related
I'm trying to make sure that at least 4 alphanumeric characters are included in the input, and that underscores are also allowed.
The regular-expressions tutorial is a bit over my head because it talks about assertions and success/failure if there is a match.
^\w*(?=[a-zA-Z0-9]{4})$
my understanding:
\w --> alphanumeric + underscore
* --> matches the previous token between zero and unlimited times ( so, this means it can be any character that is alphanumeric/underscore, correct?)
(?=[a-zA-Z0-9]{4}) --> looks ahead of the previous characters, and if they include at least 4 alphanumeric characters, then I'm good.
Obviously I'm wrong on this, because regex101 is showing me no matches.
You want 4 or more alphanumeric characters, surround by any number of underscores (use ^ and $ to ensure it match's the whole input ):
^(_*[a-zA-Z0-9]_*){4,}$
Your pattern ^\w*(?=[a-zA-Z0-9]{4})$ does not match because:
^\w* Matches optional word characters from the start of the string, and if there are only word chars it will match until the end of the string
(?=[a-zA-Z0-9]{4}) The positive lookahead is true, if it can assert 4 consecutive alphanumeric chars to the right from the current position. The \w* allows backtracking, and can backtrack 4 positions so that the assertion it true.
But the $ asserts the end of the string, which it can not match as the position moved 4 steps to the left to fulfill the previous positive lookahead assertion.
Using the lookahead, what you can do is assert 4 alphanumeric chars preceded by optional underscores.
If the assertion is true, match 1 or more word characters.
^(?=(?:_*[a-zA-Z0-9]){4})\w+$
The pattern matches:
^ Start of string
(?= Positive lookahead, asser what is to the right is
(?:_*[a-zA-Z0-9]){4} Repeat 4 times matching optional _ followed by an alphanumeric char
) Close the lookahead
\w+ Match 1+ word characters (which includes the _)
$ End of string
Regex demo
I suggest using atomic groups (?>...), please see regex tutorial for details
^(?>_*[a-zA-Z0-9]_*){4,}$
to ensure 4 or more fragments each of them containing letter or digit.
Edit: If regex doesn't support atomic, let's try use just groups:
^(?:_*[A-Za-z0-9]_*){4,}$
How shall we write a regular expression to validate a string that should have a length of a minimum of 1 character and a maximum of 50 characters, have both upper case and lower case, alphanumeric, include space, and have mostly used special characters like #,._-&$#? The first character should be either alphabet or number then the rest can be as mentioned above.
*If it is only one character then it should be an alphanumeric one
I have tried a regex with my limited knowledge which looks like
^[a-zA-z]*[a-zA-Z\d\-_#&$%#\s]{1,50}$
But I am not able to match the string if there is only one character given, can anyone guide me to fix this
You can use
/^(?=[\p{L}0-9])[\p{L}\p{N}_#,.&$%#\s-]{1,50}$/u
See the regex demo
Details
^ - start of string
(?=[\p{L}0-9]) - the first char must be a Unicode letter (\p{L}) or an ASCII digit
[\p{L}\p{N}_#,.&$%#\s-]{1,50} - one to fifty
\p{L} - any Unicode letter
\p{N} - any Unicode digit
_#,.&$%#- - any of these chars
\s - any whitespace
$ - end of string.
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.
Hi all I am making a password regular expression in javascript test() method, It will take the following inputs
solution
/^(?=.*\d)^(?=.*[!#$%'*+\-/=?^_{}|~])(?=.*[A-Z])(?=.*[a-z])\S{8,15}$/gm
May contains any letter except space
At least 8 characters long but not more the 15 character
Take at least one uppercase and one lowercase letter
Take at least one numeric and one special character
But I am not able to perform below task with (period, dot, fullStop)
(dot, period, full stop) provided that it is not the first or last character, and provided also that it does not appear two or more times consecutively.
Can anyone one help me to sort out this problem, Thanks in advance
You may move the \S{8,15} part with the $ anchor to the positive lookahead and place it as the first condition (to fail the whole string if it has spaces, or the length is less than 8 or more than 15) and replace that pattern with [^.]+(?:\.[^.]+)* consuming subpattern.
/^(?=\S{8,15}$)(?=.*\d)(?=.*[!#$%'*+\/=?^_{}|~-])(?=.*[A-Z])(?=.*[a-z])[^.]+(?:\.[^.]+)*$/
See the regex demo
Details:
^ - start of string
(?=\S{8,15}$) - the first condition that requires the string to have no whitespaces and be of 8 to 15 chars in length
(?=.*\d) - there must be a digit after any 0+ chars
(?=.*[!#$%'*+\/=?^_{}|~-]) - there must be one symbol from the defined set after any 0+ chars
(?=.*[A-Z]) - an uppercase ASCII letter is required
(?=.*[a-z]) - a lowercase ASCII letter is required
[^.]+(?:\.[^.]+)* - 1+ chars other than ., followed with 0 or more sequences of a . followed with 1 or more chars other than a dot (note that we do not have to add \s into these 2 negated character classes as the first lookahead already prevalidated the whole string, together with its length)
$ - end of string.
This might seem simple, but I have no knowledge in regex whatsoever. I need a regex that allows numbers, letters, hyphens and underscores only.. anything else is not allowed, so far I have:
/^[a-zA-Z\d]+$/
This matches numbers and letters but not hypens or underscores.
I don't want it to match any special characters within the string
Does anyone understand what I am doing wrong?
You say
a regex that allows numbers, letters, hyphens and underscores only
So, use
^[a-zA-Z0-9_-]+$
You can test it here.
Explanation:
^ - Start of string
[a-zA-Z0-9_-]* - Character class matching lowercase (a-z) and uppercase (A-Z) letters, numbers (0-9), an underscore (_), and a hyphen (-). Note the hyphen is at the end, and thus does not have to be escaped. + means 1 or more occurrences. If you allow an empty string, use * instead.
$ - End of string