Placeholder Image

字幕列表 影片播放

  • Hello, everyone.

  • In today's video, we're gonna be building a password generator using JavaScript, and this is a great project because it's not very difficult to do.

  • But there's a ton of different ways you can build this project, and I'm gonna be showing you the way.

  • That is, by far one of the cleanest ways to design the code so you can pick up best practice tips in JavaScript.

  • So let's get started now.

  • Welcome back to Web to have simplified my name's Kyle, and my job is to simplify the Web for you.

  • So that sounds been trusting.

  • Make sure you subscribe for more videos just like this one to get started.

  • I have an example of what we're gonna be building over here on the right.

  • And as you can see, it's just a typical password generator.

  • We can determine how many characters you want by using the scroll bar, or we can actually just and put it here.

  • We could include upper case numbers, symbols, and then when we click, generate password.

  • As you can see, it's going to generate password.

  • So obviously, to get started, we're gonna need an index dot html page to put all of our code.

  • And if we just put exclamation point and hit enter, it's gonna give us the boilerplate code.

  • We could just give it a simple title of password generator and then also, we're gonna need tohave to other files.

  • We're gonna have a job script file, so that's going to be inside of a script tag.

  • And we're just gonna call this script dot Js and make sure you defer this so that it loads after the body of your HTML and then we're also going to need a link tag and this is going to be for our CSS call it styles that CSS.

  • And let's just create those files now.

  • So we have script dot Js and we're going to have styles dot CSS just like that.

  • And now that we have that done, the first thing we can work on is our HTML and to get started, the very first thing I want to create is a container which is going to contain this blue box of the black border.

  • So we could just create a container div here and inside of here is going to be over code.

  • First, we're gonna have this password generator title, which is just going to be an H one tag.

  • And inside of it, we're just gonna put password generator hopes generate or just like that and then also underneath that we're going to have this section actually contains are generated password, and we're gonna put that inside of an H three and for now, we'll just default password.

  • So that way, when you load up the page, it's just going to say password.

  • And we can actually open this up with live server just to see what we have.

  • So far as you can see, we have our title as wolves are generated Password placeholder.

  • For now, the next thing we're gonna have is the actual form, which is going to contain all over different input elements.

  • And if you remember, we have the number of characters here.

  • So let's create a label for that, and his label is going to be inside of here.

  • It's going to say a number of characters and then for that individual label, we're gonna have an input, and this input is going to be a range input.

  • That's what gives us the slider over here that you can see and then we just want also on top of having that range in foot.

  • We want tohave a number input, which allows us to have our number over here that we can control our range with.

  • We're gonna need javascript to hook that up.

  • But for now, if we save this and go over here, you can see we have our number input as well.

  • Is this slider over here?

  • And one thing that we do need to do is actually set up some minimum values.

  • So we want a minimum of one and we wanna have a maximum here about 50.

  • And we want that both on our range and on our number input here.

  • Also, we need to give these ideas so we can select them in the Java script.

  • So what's just come in here?

  • We can say I d character amount range.

  • We're gonna do the exact same thing for a number input for just gonna call this character amount number.

  • And we can use the I d instead of our four here.

  • And by doing that now, if we save this and come over here when we were very click on our label number of characters.

  • It's gonna put our focus inside the input over here.

  • Also, let's just put some d felt values in here.

  • We're gonna save by default.

  • These are both going to be 10.

  • So we could just save all your 10 and value 10.

  • And now, as you can see, both these inputs are set to 10 by default.

  • The next thing that we need to work on is adding in all of these different check boxes, which is gonna be fairly simple or again going to have a label with a four attributes.

  • And inside of this label, we're gonna first put here include upper case, and then we're going to have an input for that.

  • And this input is going to be a type of check box.

  • And of course, we want to make sure we get this an I d.

  • We're gonna call this include upper case, and we're gonna use the same four.

  • This is going to work exactly the same.

  • Now if we click on our include uppercase little check our box for us, which is copy this for the other ones so we can come in here and we can say that we want to include numbers and we want to change this to include numbers and lastly, same exact thing.

  • But this is going to be first symbols.

  • So include symbols, cleared symbols and include symbols.

  • Now, if we look over here, we have pretty much all of the form inputs that we need.

  • The last thing we need to do is this Generate password button so we could just create a button down here.

  • We're gonna make sure that we give it a type of submit.

  • And we want to just say it generate password clips, password, just like that.

  • And now, as you can see, over here, we have all over a different form inputs correctly on the page.

  • Now, with all of that html out of the way, we can start working on the styles.

  • And the very first style I want to work on is the body.

  • So we can just select our body tag here.

  • And of course, the very first thing that we see is our background color is just going to be this really dark black color.

  • And if we say that you see, we now have that background color and of course, to make our techs stand out.

  • We want to make sure our text is going to be.

  • Wait and there we go.

  • Now we can read everything much better.

  • The next thing we want to do is center all of our content, and the easiest way to do that is by using flex box.

  • So we'll say, display flex, justify content in the center and we want to align our items in the center.

  • And if you're unfamiliar with flex box, I have an entire video covering it, which you confined linked in the cards in the description below.

  • Also, to make sure the center vertically, we need to make sure we set Amen height of 100 view height, and that's going to fill the entire height of our screen.

  • And to get rid of this annoying scroll war, all we need to do this said our margin to zero.

  • And that's going to get rid of the scroll bar for us now, lastly, so that we have a font that is a little bit interesting.

  • Look at We could just come into our fund family here, and we can just choose one of these values.

  • For example, we could just go with the Georgia fund.

  • And if we say you can see are fine, just looks a little bit more interesting.

  • It's nothing amazing, but it'll work for this application.

  • Next, we need to actually select this rapper Dave that we created, which we gave the class of container.

  • So instead of here, we could just say Doc Container.

  • And if you remember it, this is going to be this giant blue box area.

  • So we want to change the background color to be that blue color, which is 00666699 And if we say that you can see we get that blue color.

  • Also, we can change instead of here are patting to be three r e m.

  • That we have a bunch of space around our content as well as we want a little bit of a border radius so we could get those rounded corners.

  • In our case, one r e m is gonna work fine.

  • And then that black border is just gonna help it stand out a little bit from around the edges will say two pixels of solid black.

  • As you see, we have that nice looking border right now Also, we can make us that all of our content inside of here is in the center by doing the same exact flex box trick.

  • So we can say display flex, justify content in the center and a line items in the center as you can see it.

  • Now, everything is lined up in the centre, but to make it a vertical instead of horizontal, all we need to do is change the flux direction to column.

  • And as you can see, everything went back to going in that vertical direction.

  • The next thing we need to work on his style and our form inputs because they're obviously a mess right now.

  • And easiest way to do that is just to put a class on our form will just give it a class of form and we're actually gonna use CSS grid for this.

  • So if you're unfamiliar with CSS cred, I again have an entire video on it and the cards and the description.

  • So you make sure you check that out.

  • But essentially, we want to change here the display to grid and we want to modify the grid template columns and we want there to be two separate columns, one for the label, as you can see here, and one for the value section.

  • We want them both just to be auto sized.

  • So we're just going to say great temple columns, auto and auto.

  • As you can see, we now have two columns, but there's a problem.

  • And as because our range slider and our input are on two separate rose, we want them to take up one column instead of two different columns.

  • So to do that, we're gonna wrap them in a div.

  • So that way that Dave is going to span an entire column, and these objects inside of it are going to be in that column.

  • Now, if we save, you can see that our slider and our input are both on the same column.

  • But everything is really scrunched up right next to each other.

  • So in order to fix that, gonna add a little bit of a gap.

  • First, we're gonna add a gap on the road, or you're gonna use one r e m.

  • And we're gonna use a column gap in between our columns of three r e m magic.

  • You see, they're really spaces out.

  • Our elements and looks a lot better.

  • The last thing we need to do, just make sure that our grid is centred, Soul said.

  • Justify content centered in a line items in the centre.

  • Now that'll make sure that our group is always inside the center of its container instead of stretching or being pushed around.

  • Now what we need to do is actually style are different elements.

  • So, for example, this password generator title.

  • So let's just add a class to that of title clips title Just like that.

  • And now, inside of our style sheet, we can select that title and weaken first to remove the margin from it.

  • We can also make sure we text the line it in the center just so everything is in the center and there we go.

  • You can see that just made that her password generator title is a little bit more squished up with our display password.

  • Also, we should bold these labels so they're easier to read so we can just say label.

  • We want to make all of those a funt weight of bold magic.

  • You see, that just made them a little bit easier to read its expander screen a little bit, we go.

  • Another thing that you may notice is that our input box here is not centered with our range letter, and it's also very wide concerning the maximum number.

  • It's 50.

  • It'll only ever be two digits long.

  • So in order to fix that, we could just put a class on this rapper container.

  • We're gonna call it character amount Container.

  • We could just copy this selector, and instead of our style shoot, we can use that selector and all that we need to do is actually make sure that we set it to display of Flex.

  • And we want to align our items in the center and you'll see that's going to align these in the centre vertically just like that.

  • And then we can select our input instead of here are number input.

  • Swisher's at a class to that.

  • What is just going to be number input?

  • And instead of our style of shooting, we could just say Oops, number input.

  • All we want to do is set the with to be to R E M, which is going to be about the width of two characters, as you can see here and that looks a lot better now.

  • Now there are only two elements left on our page to style, and one of those is going to be our password input section Solo again was just at a class to that.

  • We can come in here.

  • We can say that our class, his password, password display and let's copy over that class and what we need to do inside this password display is first change the background color toe.

  • Wait because, as you can see, it's white will say background color of white.

  • Make sure we intend that properly.

  • There we go, and we also want the color of this to be black.

  • It's that way the text will show, as you can see over here now we have a white background and the black text.

  • Also, we had to add a little bit of padding because right now that's way too squished.

  • Ups will say one R e m of padding.

  • We're gonna add a border around this of one pixel, and we're gonna use the same color as our background never go.

  • That looks pretty nice, and we also want to make sure we manually set the height and width of this.

  • So that way, if we change the size of our password, it's not gonna make our box grow and shrink.

  • So what we're gonna do is we want to make sure we have a height here of two R e m essentially that allow us to fit two rows of text and we also want to sit in here with which is just going to be 350 pixels.

  • And as you can see, that box is going to take up essentially the full width of our container, which looks really nice.

  • Next, we're going to use that same exact display flex trick that we used all across this application to center everything so well just a line, everything in the center.

  • And that's going to make us that our text always shows up in the center just like this.

  • And we also want to make sure we set the world break to break all.

  • And this means that if our password extends past the end of the line, it'll just break in the middle of the line like this and go on to the next row.

  • Also, what's set a really small quarter radius, just we have that nice looking rounded corner.

  • We'll save 0.5 r e m.

  • And if we go back to wrap, you can see this is already coming together.

  • Now, the last thing is this generate password button so we could just put a class on our button.

  • Well, just give it a B t m class.

  • And now, instead of our style shoot, we can select that b team class.

  • But inside here, what we want is this to span two columns because we want to take up the entire width on the bottom, as you can see over here.

  • So we can just say that we want our grid column to span, too.

  • And that's just going to make it span two columns like that, as you can see, also, we want to get rid of our background color so we can come in here saying background color is going to be transparent.

  • That just makes it so we can see through a button like this.

  • Also, we're gonna set a border on this on our border is going to be two pixels of solid.

  • Wait, there's going to give it that outline, which is looking really nice.

  • We also want to make our text color to be white.

  • We're gonna need to put a bunch of padding on this swill, say 20.5 r E m and one r e m That space is that and looks pretty good.

  • A little bit hard to read those up the front, wait to be bold and never go.

  • That's a little bit easier to read.

  • Changed the cursor to be Pointer That just makes it somebody hover.

  • You get that little pointer icon instead of the normal icon.

  • And then the last thing that we need to do is just, of course, adding our border radius.

  • We'll just do one r e m, which is going to round our corners all the way around.

  • Now, the last thing you want to add a hover state to this and inside of a hugger state, all we're gonna do is change the background color, and we want to make our background just a slightly transparent white color.

  • That way we can get our blue to shine through.

  • So we'll just say six chefs, which is going to be white, and then 33 is gonna give us a mostly transparent wait as you can see when we hover that it's a mostly transparent wait which shows up is this nice light blue color and that's all of the CSS for our form.

  • And now we can move on to the fun part, which is going to be the job script.

  • So the first thing that we need to do is make it so that our slider links up with our number input over here.

  • And to do that, we can just use our I D selectors from our job from our HTML.

  • So we have our character, a mountain range, so we can say constant character Mountain range is equal to our document.

  • Get element by i d.

  • We just pass it in that character amount range.

  • We could do the exact same thing, but for our character amount number.

  • So we can just put character, amount, number and character Mountain number just like that.

  • And now we can set up event listeners on those so our character amount number add event listener.

  • We want to say on input, which is any time that our input changes instead of her box.

  • We want to call a function, and we're just gonna call this sink character amount.

  • We went and did the exact same thing on our range input.

  • So any time we change our range input, we also want to make sure that we call the exact sand function.

  • And now we can create that function.

  • And this function, of course, is going to take our event argument and we can get our value, which is just going to be here ee dot target dot value.

  • Then we can set our character amount number that value equal to that value.

  • And we could do the same thing with our range just like that.

  • And now when we change our slider, you see, it'll change our input.

  • And when we change our input, you see it'll also changer Slater.

  • They're linked to each other, which is really nice.

  • The next thing we need to do is set up or form so that when we click generate password, it'll actually generate a password instead of just refreshing our page.

  • So the easiest way to do that is if we put on an I d on our form will just say, Here i d it's going to be equal to password generator greater form just like that.

  • And now inside of our JavaScript, we can say that our form, it's going to be equal to document dot get element by i d of password generator form.

  • And then we can set up a simple event.

  • Listener, we want to listen for the submit event.

  • So any time we try to submit our form, we're gonna call this function.

  • It's going to take an event, and all we want to do is say, ee dot prevent default.

  • This is going to stop our form from submitting and refreshing our page.

  • So now if we change this click generate password, you see that nothing's actually happening because we're preventing the default, but not actually doing anything.

  • The next thing we want to do in here is you want to get a password, variable.

  • We're gonna get it from some function which we're just gonna call, generate password, and this generate password function is going to take the number of characters so we can say character amount.

  • It's going to take if we want to include uppercase.

  • It's also going to take it.

  • We include numbers, and lastly, it'll take if we include symbols.

  • So to get these variables.

  • We just need to pull them from the values inside of our page here.

  • So we get our character amount from our character amount number so we can say character amount is equal to character amount number that value.

  • But we need to get selectors for all of our other elements.

  • So, as you see, we have our include upper case I d.

  • For input here.

  • So we can just say constant include uppercase element equals document that get element by i d.

  • And we'll just pass it in that i d and then it's headed here we can sit that are include up.

  • Her case is going to be equal to include other case element dot checked and this just tells us true or false, if the button is checked and we can do this two more times, we want to do this for include numbers and we also want to do this for include symbols and we're gonna name the variables Zack with same.

  • So this would be include numbers element, and this is going to be include symbols element, and we can just copy this upper case so this will be included.

  • Numbers make sure that's spelled correctly.

  • So include numbers.

  • And this won't be included, Symbols just like that.

  • And of course, we need to change it over here to include numbers and include symbols.

  • Now, when we called this function which we haven't created yet, we're gonna be passing it hold of values from our form here that we have based on our check boxes and based on our input from our characters.

  • So let's just create that function, generate password, and as we know it's gonna take a character amount it's going to take include upper clase, include numbers and include symbols.

  • And inside of this function, we're gonna do over code for generating our password.

  • And this right here is really where you can go a ton of different ways with the code.

  • But I'm gonna show you the cleanest way to write this code, and that is using character codes.

  • So if we pull up this shit over here, this is going to be all of the character codes for all of the asking characters.

  • And as you can see, we have our upper case.

  • Numbers are lower case numbers.

  • We have our numbers here as both all these different special symbols that we can use on.

  • What we can do is we can actually change from this number 65 to this string character by just using string dot from char code.

  • If we pass in 65 here, it's actually going to give us an uppercase A.

  • We passed in 82 is gonna give us an uppercase are and so on.

  • So if we create a list of all the character codes that we can use, that we could just write a simple four loop and loop over at a bunch of times to create all of the characters of our password, and that is so much easier than writing out an array that says a and then it's going to have be inside of it and so on like this.

  • That takes a lot of work, and it's not very clean.

  • So in this way, we're gonna have a much cleaner code set up by just using these numbers instead of using an array of strings.

  • And we can actually use a function to generate our race for us so we can just say Ray here from high too low, we're gonna pass it in the low value and the high value.

  • What's actually just name this from low to high makes a little bit more sense.

  • There we go.

  • And inside of here, all we need to do is create a simple four loop that's going to go from a variable I that is equal to low.

  • It's going to add one each time until it gets to the point that it is the high number just like this.

  • So essentially, we're looping through all of the different variables between our low number and our high number.

  • And we're gonna put this inside of this empty array right here.

  • So we could just say a ray that push and we want to push and I and then, of course, return our array.

  • So now this function, generous hosts an array from our low number Toa are high number, so appear we can create a variable.

  • We're gonna call this upper case char codes.

  • This is going to be equal to array from Glo tie.

  • And as you can see, 65 is our lowest upper case number, and our highest upper case number is 90 down here.

  • So let's put in 90.

  • And now we have all over uppercase character codes inside of this list from 65 to 90.

  • Let's do the same thing with the lower case, because by default we're gonna use the lower case passwords.

  • And that's 97 to 1 22 So, he said, 97 1 22 just like that.

  • And now we already have.

  • A lot of it could set up.

  • Let's do our numbers as well.

  • This is 48 57.

  • So let's copy this saying number character codes.

  • And that is going to be, as I mentioned, hoops 48 2 57 Now, lastly, we have our symbol character codes symbol, and this is going to be slightly differently because they're actually quite a few gaps.

  • First, we have 33 to 47 which is a bunch of different symbols, so we can come in here 33 to 47.

  • But we want to make sure we can.

  • Captain ate a new array on to that because we also want to get all these values for 58 to 64th so we can say the exact same thing all of these arrays.

  • But what is it from 58 to 64.

  • We also want to get these values from 91 to 96.

  • Something can do another can cap nation.

  • And we want to get the arrays here from the 91 to 96.

  • And then lastly, we want to get these 1 23 toe 1 26 So again, what is gonna captain eight?

  • That on this is going to be an array from low to high of 1 23 toe 1 26 Now we have all over different arrays of character codes.

  • And to show you what's actually working inside of here, I'm just gonna counsel dot log out, for example, are lower case character goes.

  • And now if I inspected this and I click on degenerate password button, you can see we get all of our character Coates 65 to 90 which is all over lower case character codes.

  • And this is the same for all of our different arrays here that we created for lower case over case numbers and symbols.

  • And now in this function, we can create an overall list of all of our character codes and then loop through and just randomly choose one each time so the first thing we want to do is set up our variable, which is going to be here are character codes so we can say char codes and we want to default this toe are lower case character codes and then we want to depend on our include uppercase.

  • So if we, for example, are including our upper case than now, chart Coz is going to be equal to chart codes dot Can cat are uppercase character codes.

  • Just like that, we can do the exact same thing for our numbers.

  • So let's say include numbers and we want to come cabinet on lips are number character codes just like that.

  • And lastly, we want to do include symbols, and this is going to be our symbol character codes.

  • Now our character codes is going to be a list of all the possible character codes that we can generate.

  • And all we need to do is just loop for all of our different character maps so we can just come in here.

  • We can say what I equal zero when I is less than our character amounts, flips character amount, and we just want to add one each time this is going to look once for each character and we're gonna need a variable, which is going to store all of our passwords.

  • So it's a password care ric characters that's just going to be an empty array.

  • And then, instead of relief, would you just say password characters dot push And we want to push our character.

  • But in order to get that character, we just need to get a random value from our list of character codes, as you can see here.

  • So we can say our character, I was going to be equal to character codes and we want to get a single index on what we're gonna do is say, math 0.4 of Matt Thought random.

  • And what we want to do is we want to get a random number, and then we want to multiply that random number times our character amount, and now this code looks a little bit confusing.

  • But essentially what it's doing is this getting in a random number between zero and our character amount, and then it's making sure that it's an imager by doing math out floor, and then it's going to give us our character code So this is actually going to be a character code here.

  • What we want to do is actually change our character code by doing string dot from char code, we're gonna pass in our character code just like that.

  • Now we can return our password characters.

  • We want, of course, turn that into a string.

  • So we're gonna join on an empty string.

  • But essentially, he's going to turn our right into a string.

  • And instead of here, all we need to do is just set our password text here.

  • So what's come back into here and give our password display an I.

  • D of password display that would be constructed in our JavaScript and we can come in here.

  • This is going to be our password display, and we want to set it to her password display I d.

  • And then we can say password display dots in her text is just going to be equal to our password.

  • And now, if we click, generate password, you can see we get 10 characters here.

  • But obviously these are upper case instead of lower case.

  • So it looks like I have our character codes swapped.

  • So these they're gonna be our lower case.

  • These are going to be our upper case.

  • And now let's check that.

  • You can see we get lower case.

  • If we include the uppercase characters, you can see we're gonna get a combination of uppercase and lowercase.

  • But of course, that's also not working.

  • So it looks like somewhere in our code down here we have a problem.

  • And if we take a look here, it looks like the issue is that I'm actually using character amount.

  • This is only given us a number between one and the number of characters we specify, but we actually want to get a number between one and our chart codes dot length.

  • This is essentially going to give us any character inside this character.

  • Coates array.

  • Now, if we include uppercase click generate, you can see we're getting up a case and lower case.

  • If we do, numbers will scale this up.

  • You can see we're getting some numbers inside of here.

  • We have a seven there, for example.

  • We do it again, we get even more numbers.

  • And lastly, if we include symbols were getting a bunch of symbols of her case, lower case and numbers, which is an incredibly secure password.

  • And that's all there is to this really simple password generator.

  • If you enjoyed this video, check out my other videos winked over here.

  • I subscribe to the channel for more videos just like this one.

  • Thank you very much for watching and have a good day.

Hello, everyone.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

用JavaScript建立一個密碼生成器 - 教程 (Build A Password Generator With JavaScript - Tutorial)

  • 8 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字