WEBVTT 00:00:06.990 --> 00:00:16.980 Know we're going to use the try and cut statements to validate a password in boots and show error messages 00:00:17.430 --> 00:00:18.480 on the web page. 00:00:18.710 --> 00:00:19.310 OK. 00:00:19.530 --> 00:00:23.950 So let's create first our inputs before we do that. 00:00:24.360 --> 00:00:28.860 This creates a paragraph. 00:00:29.220 --> 00:00:32.900 OK. 00:00:36.500 --> 00:00:51.180 OK so the program is going to be saying your password should contain at least six characters one number 00:00:52.080 --> 00:00:59.670 and one copy so that's it. 00:01:02.260 --> 00:01:02.500 OK 00:01:05.920 --> 00:01:10.390 then we're going to have another paragraph. 00:01:10.820 --> 00:01:27.350 So this one is going to be Enter password followed by a line break and then the password inputs. 00:01:27.370 --> 00:01:27.960 OK 00:01:33.410 --> 00:01:44.130 so are going to give it an ID password and a type of password. 00:01:45.890 --> 00:01:46.720 And another. 00:01:46.730 --> 00:01:48.240 So this time re-enter 00:01:50.880 --> 00:01:53.150 password. 00:01:54.090 --> 00:01:58.900 The ID is going to be password to. 00:01:59.220 --> 00:02:03.120 OK. 00:02:03.630 --> 00:02:08.670 All right. 00:02:09.010 --> 00:02:13.170 Also we need a button a submit button so 00:02:19.590 --> 00:02:22.730 button OK. 00:02:23.430 --> 00:02:28.860 And when we click on the button we're going to run a function that we will find later and his function 00:02:28.860 --> 00:02:31.520 is going to be check. 00:02:31.620 --> 00:02:35.720 Let's put some text there submit. 00:02:35.790 --> 00:02:38.580 See he's working. 00:02:38.910 --> 00:02:46.270 OK so for now let's just commence the previous code they've done. 00:02:46.380 --> 00:02:46.770 All right 00:02:49.710 --> 00:02:51.310 OK. 00:02:51.330 --> 00:03:00.050 Now we can define the function check which will validate the user input. 00:03:00.060 --> 00:03:01.710 OK. 00:03:02.660 --> 00:03:03.210 All right. 00:03:03.280 --> 00:03:09.490 So we're going to use the key word function. 00:03:09.510 --> 00:03:13.150 Then the name is check. 00:03:15.270 --> 00:03:23.310 OK so first of all we're going to declare a couple of variables password and password to to retrieve 00:03:23.310 --> 00:03:27.230 the values of the two passwords. 00:03:27.240 --> 00:03:30.500 So first one is password. 00:03:31.830 --> 00:03:39.460 And he's going to be documents gets elements spy I.D. and we're looking at password. 00:03:40.950 --> 00:03:41.920 OK. 00:03:42.260 --> 00:03:52.890 And looking at the volume and then the next one is going to be password to be looking is the password 00:03:53.040 --> 00:03:54.550 to OK. 00:03:54.550 --> 00:04:01.940 Also we did define another variable error message. 00:04:03.180 --> 00:04:05.250 OK. 00:04:06.030 --> 00:04:14.240 Which is going to be the value of the error program. 00:04:15.270 --> 00:04:19.320 The program itself and then we can access the Shamal later. 00:04:19.620 --> 00:04:20.370 OK. 00:04:20.370 --> 00:04:24.270 So then one last one we need another variable. 00:04:24.270 --> 00:04:33.990 It's going to be error that we're going to be throwing So error to zero. 00:04:34.280 --> 00:04:37.000 So at the moment it's just an empty string. 00:04:37.740 --> 00:04:39.500 All right. 00:04:39.540 --> 00:04:44.720 Now we're going to have our Troy statesman's and catch statesman's. 00:04:44.970 --> 00:04:55.080 So OK so Troy statesman's and then we're going to put the piece of code in the Troy statements and then 00:04:55.410 --> 00:05:05.050 we're going to have a clutch statements and then we'll put the other piece of code inside the code statement 00:05:05.070 --> 00:05:05.300 . 00:05:05.500 --> 00:05:07.380 OK. 00:05:07.620 --> 00:05:13.940 All right so so first thing is the password should include at least six characters. 00:05:13.940 --> 00:05:16.910 We're going to be checking the length of the password. 00:05:16.990 --> 00:05:24.570 Also we need to check that there is one copy of the letter and also one number and finally we need to 00:05:24.570 --> 00:05:28.380 make sure that the two passwords they match. 00:05:28.380 --> 00:05:33.160 So let's have look at the first condition of the length of the password. 00:05:33.390 --> 00:05:48.610 So it's going to go and say something like this so if the password lends is less than 6 ok 00:05:53.760 --> 00:05:55.650 then the error 00:05:58.440 --> 00:06:00.940 to throw. 00:06:00.940 --> 00:06:01.440 OK. 00:06:01.560 --> 00:06:03.720 We're going to add to it. 00:06:03.750 --> 00:06:06.060 First of all a line break. 00:06:06.810 --> 00:06:07.580 OK. 00:06:07.650 --> 00:06:12.790 And you going to say password too short. 00:06:13.280 --> 00:06:13.910 OK. 00:06:14.130 --> 00:06:15.310 So this is the first one. 00:06:15.740 --> 00:06:18.640 OK. 00:06:19.000 --> 00:06:26.250 The other condition is so we need to make sure that the password is called One copy the letters so we 00:06:26.250 --> 00:06:30.250 get to use the test method. 00:06:30.440 --> 00:06:30.690 OK. 00:06:30.690 --> 00:06:32.460 And we need the rejects here so. 00:06:32.460 --> 00:06:41.800 So if so the rejects is going to be for slides and then in between brackets copy a letter a to capitalism 00:06:41.940 --> 00:06:47.110 said OK and we're doing a global search. 00:06:47.250 --> 00:06:50.550 OK. 00:06:51.490 --> 00:06:57.180 And then those tests and I was trying here is the password. 00:06:57.460 --> 00:06:58.310 OK. 00:06:58.710 --> 00:07:00.400 So if this is false 00:07:06.610 --> 00:07:16.200 then we're going to run this piece of code which is to add to the error to throw again. 00:07:16.230 --> 00:07:24.250 We're going to do something similar to this and he's going to say passwords should include at least 00:07:24.250 --> 00:07:29.840 one capital letter. 00:07:34.530 --> 00:07:37.730 OK. 00:07:38.040 --> 00:07:45.240 And then the next one is going to be testing whether the password is codes at least 1 digits it's going 00:07:45.250 --> 00:07:46.660 to be a similar code. 00:07:46.890 --> 00:07:51.940 So rather than having these rejects the syntax is going to be backslash 00:07:54.690 --> 00:07:56.890 and small the four digits. 00:07:57.050 --> 00:07:58.340 OK. 00:07:58.510 --> 00:08:04.610 And the message is going to be at least one deletes. 00:08:04.770 --> 00:08:13.170 And finally we need to make sure that the two passwords are the same. 00:08:13.170 --> 00:08:13.740 So a 00:08:17.160 --> 00:08:23.020 password is different from password too. 00:08:24.000 --> 00:08:25.390 OK then 00:08:28.280 --> 00:08:30.560 we're going to add the error to throw 00:08:38.270 --> 00:08:40.100 a passwords. 00:08:40.260 --> 00:08:44.880 They should match. 00:08:49.240 --> 00:08:50.990 OK. 00:08:51.210 --> 00:08:58.490 If we use the throw error to throw in one of the codes here then it will show us only one problem. 00:08:58.800 --> 00:08:59.600 OK. 00:08:59.980 --> 00:09:01.050 So we can try it and see. 00:09:01.050 --> 00:09:04.270 So let's try it in the first statement so 00:09:07.500 --> 00:09:13.510 no error to throw. 00:09:13.700 --> 00:09:20.980 We can try in all the other statements. 00:09:22.380 --> 00:09:30.750 Now then inside the country statements we are going to change the in a symbol of error message to the 00:09:30.750 --> 00:09:33.490 value that we have thrown. 00:09:33.890 --> 00:09:34.690 OK. 00:09:35.060 --> 00:09:38.500 Does bear in mind that this time we are throwing a string. 00:09:38.760 --> 00:09:39.280 OK. 00:09:39.370 --> 00:09:41.040 Which is thought inside the variable. 00:09:41.080 --> 00:09:42.120 Eric to throw. 00:09:42.540 --> 00:09:48.640 And then that string is going to be stored inside the variable E R R. 00:09:48.880 --> 00:09:49.440 OK. 00:09:49.810 --> 00:09:57.410 So unlike the first example where e r r was an object because we didn't specify anything to throw. 00:09:57.420 --> 00:10:04.440 So by default your arm was an object and to get the error message we had to use the message property 00:10:05.280 --> 00:10:09.490 of the objects are in our second example. 00:10:09.640 --> 00:10:11.930 We r is a string. 00:10:11.970 --> 00:10:13.990 Because we are throwing a string. 00:10:14.250 --> 00:10:23.370 Ok so if we throw something specifically then that's variable or the value of that variable is going 00:10:23.380 --> 00:10:26.800 to be stored inside the R R variable. 00:10:27.150 --> 00:10:35.200 Ok so to change the in of our error message we're going to use the variable we are straight away. 00:10:35.620 --> 00:10:37.360 OK. 00:10:37.360 --> 00:10:37.770 All right. 00:10:37.840 --> 00:10:46.500 Let's refresh the page and try a couple of buzzwords that don't fulfill the conditions. 00:10:46.620 --> 00:10:49.340 OK so I'm going to try. 00:10:49.810 --> 00:10:53.730 1 1 1 and 2 2 2. 00:10:54.120 --> 00:10:54.860 OK. 00:10:54.880 --> 00:10:57.370 So realistically can submit. 00:10:59.010 --> 00:11:01.480 So indeed the buzzword is too short. 00:11:01.710 --> 00:11:04.220 But we were expecting more than one error. 00:11:04.640 --> 00:11:05.410 OK. 00:11:05.860 --> 00:11:13.380 And the reason why we got only the first error which is password to Chwat is because when we run the 00:11:13.380 --> 00:11:22.790 code once we run a through statements then we go out of the try statement. 00:11:22.810 --> 00:11:24.200 OK. 00:11:24.340 --> 00:11:29.350 So we should not have many throw statements like this. 00:11:29.350 --> 00:11:32.400 In our case inside the trye statements. 00:11:32.430 --> 00:11:41.320 So we are going to do we are going to delete all these throw statements and have only one throw statements 00:11:41.910 --> 00:11:45.250 at the end of the try statements. 00:11:46.260 --> 00:11:46.510 OK 00:11:48.330 --> 00:11:50.800 . 00:11:51.120 --> 00:11:55.750 In a place it's right here. 00:11:56.250 --> 00:11:57.640 OK. 00:11:57.630 --> 00:12:08.710 So let's save and refresh the page and this time if we try 1 1 1 and then 2 2 2 and we submits then 00:12:08.700 --> 00:12:12.530 we can see all the error messages that we were expecting. 00:12:13.060 --> 00:12:14.680 All right. 00:12:14.860 --> 00:12:19.350 Obviously you can build the same application using only if statements. 00:12:19.510 --> 00:12:25.600 I just wanted to show you an example of how we use the try and catch statements. 00:12:25.940 --> 00:12:26.150 OK 00:12:26.170 --> 00:12:36.450 .