-1

Here is a link to a website im using to validate and check the regex is working : https://regex101.com/

Here is the regex im using so far :

/(?=[\x21-\x7e]{8,20})(?=[^0-9]*[0-9])(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z]).*/g

Heres the text im testing :

"1abcderfggdgf sdvhbsdifsdfsdf1 Ghhuidicbdbuhbdc bhdbcdbebvuheY uuvvvyuv1G 1Guhuuuyuyuby Y%*&^$^%^(^(GVGVYUKVYTUHBKBUFygyygyg

ebfuiuberiueu23423HHII"

This is what i think this is doing :

  1. Looking to match any ascii character between 33 and 126 indexed and is between 8-20 in length
  2. look for a non number that precedes a number 0 or more times (from what ive read apparently this detects if there is a number present)
  3. Check for lower case letter that is preceded my 0 or more non lower case letters.
  4. do the same thing ^^ with upper case letters
  5. throw it an any character using ".*" 0 or more times because with the look aheads they should narrow it down so not every character is allowed and it should match what the look aheads select

This is what I want it to do :

validate passwords that follow these specifics -

  • Must have at least 1 lowercase letter
  • Must have at least 1 uppercase letter
  • Must have at least 1 number
  • can only be between and including 8 - 20 characters in length
  • characters can only be from and including 33 to 126 in ascii NOT including white-space characters (where did i get this from? - https://kb.wisc.edu/page.php?id=4073)

Extra Notes :

Ive been using the global (?thing) on the end of the regex to specify not to stop at the first match I think for my password field I dont need this because it should only be limited to 20 characters anyway.(im not sure about this)

also itd be cool to detect whether a character has been used consecutively more then 3 times but its not necessary at the moment.

The password is input to a form in my php file. Im trying to use this as a validator for an input tag.

there is no code to not allow spaces as of yet.

Ahurasim
  • 27
  • 1
  • 8
  • 1
    Your regex looks on the right track at least. What is your exact question? – Tim Biegeleisen Oct 10 '17 at 04:56
  • My question is What is the right regex for what im looking for and how does it work ive seen others online that come close to this but they all have the start of string "^" and end of string "$" characters which when on dont seem to match anything when in the code – Ahurasim Oct 10 '17 at 05:05
  • What does the word "im" in the title mean? –  Oct 10 '17 at 05:07
  • is that a dig at me? Im sloppy way of writing I'm or I am. – Ahurasim Oct 10 '17 at 05:25
  • if you want to include whitespace restrictions `(?:^|(?<=\s))(?=\S*[0-9])(?=\S*[a-z])(?=\S*[A-Z])[\x21-\x7e]{8,20}(?:(?=\s)|$)` working example [here](https://regex101.com/r/0FLBmM/1/). Also I put beginning and ending conditions as either whitespace or endings, you can change them. I put them as is as your input was a series of strings... – kaza Oct 10 '17 at 05:48
  • Good news: there is no need for such password rules. Recently NIST published an [official paper](https://pages.nist.gov/800-63-3/sp800-63b.html), advising against such rules, against its former recommendations. Complex password rules will usually not lead to more safe passwords, important is only a minimum length. People cannot remember tons of strong passwords, and such rules can interfere with good password schemes. People can get very inventive to bypass such rules, e.g. by using weak passwords like "Password-2017". Often you end up with weaker passwords instead of stronger ones. – martinstoeckli Oct 10 '17 at 14:21
  • @martinstoeckli Cool information I think I came across something similar said elsewhere but i reckon ill stick to the setup I already made as I pretty much coded it all in already maybe next time Ill consider that as im going to be helping my dad put up a website we'll see what happens :) – Ahurasim Oct 11 '17 at 14:17
  • 1
    @guest271314 got your comment I cant vote yet and after reading that I realize this comment is probably not encouraged either still think my thanks was valid even though it goes against the sites guidelines as you just wrote up a long helpful answer for me as did the other person guess when I get enough points I can upvote as a sign of thanks lol – Ahurasim Oct 11 '17 at 14:22

2 Answers2

0

The only "bug" I saw is that you are trying to limit the length without using anchors - you are allowing 20 characters to match, but you don't check you've matched the whole input. This should work:

(?=^[\x21-\x7e]{8,20}$)(?=[^0-9]*[0-9])(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z]).*

Working example (click on "Run Tests"): https://regex101.com/r/L4uia8/3/tests

This is a matter of style, but I tend to prefer to replace .* with the more significant lookahead, as such (this is similar to removing and true in conditions):

^(?=[^0-9]*[0-9])(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])[\x21-\x7e]{8,20}$

Working example: https://regex101.com/r/L4uia8/4/tests

In general, you shouldn't limit the length of passwords.

Kobi
  • 135,331
  • 41
  • 252
  • 292
  • Anchors thats what they are called (I didnt remember my bad) and yes i was having trouble with them not working. the answer above seems to almost work except it is matching strings on lines with no capital letter and also not matching "Y%*&^$^%^(^(GVGVYUKVYTUHBKBUFygyygyg ebfuiuberiueu23423HHII" I see you used multiline on it so it should match these i think. – Ahurasim Oct 10 '17 at 05:14
  • @Ahurasim - I'm not sure what you mean. These two lines are not matched by my pattern - I've included an example here: https://regex101.com/r/L4uia8/1 – Kobi Oct 10 '17 at 05:17
  • OHH :) I just realised the last two shouldnt match because the first 20 characters dont have what is required to match is this correct? and what of the first two that do match they are still a problem – Ahurasim Oct 10 '17 at 05:18
  • @Ahurasim - That's because of the newline. Try this: https://regex101.com/r/L4uia8/3/tests – Kobi Oct 10 '17 at 05:25
  • Cheers for this I checked and it appears to work it although you didnt omit the spaces I accepted the answer and after a talk with a friend it was suggested I allow spaces anyway for anyone looking to omit spaces bulbus has posted a comment on that above – Ahurasim Oct 10 '17 at 09:00
0

You can use a RegExp for each of

  • Must have at least 1 lowercase letter
  • Must have at least 1 uppercase letter
  • Must have at least 1 number
[/[a-z]/, /[A-Z]/, /[0-9]/]

create a string having characters to match

  • characters can only be from and including 33 to 126 in ascii NOT including white-space characters
[match] = Array.from({length:126 + 1 - 33})
.reduce(([key, prop]) => 
 [key += String.fromCharCode(prop), ++prop], ["", 33]))

check if character in string is within range of valid characters

 && [...str].every(s => match.indexOf(s) > -1)

and check .length of the string

&& str.length.length >= 8 
&& str.length < 21;

const check = (str
              , [match] = Array.from({length:126 + 1 - 33})
                .reduce(([key, prop]) => 
                  [key += String.fromCharCode(prop), ++prop],["", 33])
              , re = [/[a-z]/, /[A-Z]/, /[0-9]/]
              , minLength = 8
              , maxLength = 20 + 1) => 
                  re.every(regex => regex.test(str)) 
                  && [...str].every(s => match.indexOf(s) > -1)
                  && str.length >= minLength
                  && str.length < maxLength;

let [...strings] = ["aB1!!@{@@}}", "Def//3\\21", "gHI//0 \12"];

strings.forEach(str => console.log(check(str)))
guest271314
  • 1
  • 15
  • 104
  • 177
  • It is a good point that you can express some of these requirements by code, but you have several problems here. `/[A-Z]` is missing `/`,`str.length.length` is undefined, and the character range check is illegible ([`every`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) on [each character](https://stackoverflow.com/q/4547609/7586) would fit better here). – Kobi Oct 10 '17 at 05:37
  • @guest271314 just wanted to thank you for your answer even though i accepted the other persons answer I think this is a good one as well :) if i could like it somehow I would (not too familiar with SO's points system yet) – Ahurasim Oct 10 '17 at 08:33