Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,100 --> 00:00:05,150
Now that are contact us section is styled properly.
2
00:00:05,270 --> 00:00:12,050
We can go ahead and start working on the actual inputs and we're going to go ahead and validate that
3
00:00:12,050 --> 00:00:18,840
these four inputs have only valid values and what's come down in the code.
4
00:00:18,840 --> 00:00:25,710
Editor in contact at DOT J.S. and we'll scroll down to where we have the text field components and if
5
00:00:25,710 --> 00:00:33,540
you remember from the documentation we have the error prop available to us if I go ahead and on the
6
00:00:33,630 --> 00:00:35,030
name text field.
7
00:00:35,130 --> 00:00:40,320
I'll go ahead and just add the error prop as it is and that will default it to true.
8
00:00:40,320 --> 00:00:43,870
So if I go ahead and save that you'll see the page refresh.
9
00:00:44,100 --> 00:00:50,190
And now we have the red error styled input when we have an error.
10
00:00:50,190 --> 00:00:56,130
We'd also like to go ahead and give them a little information about why the error is occurring and we
11
00:00:56,130 --> 00:01:04,320
can do that using the helper text prop and if we go ahead and set this equal to a string like Please
12
00:01:04,770 --> 00:01:10,950
enter a name go ahead and save that and you'll see the page refresh.
13
00:01:10,950 --> 00:01:18,130
And now it's got a little bit of text underneath the input telling you why the error is occurring.
14
00:01:18,270 --> 00:01:18,570
All right.
15
00:01:18,600 --> 00:01:24,870
But one thing to note here with the help or text is if I go ahead and remove the arrow prop and go ahead
16
00:01:24,870 --> 00:01:32,850
and save the file once the page refreshes you'll see that the helper text is still there it is just
17
00:01:32,850 --> 00:01:35,710
not receiving the error styles.
18
00:01:35,760 --> 00:01:41,460
So I would actually like to only apply the helper text if there is an error.
19
00:01:41,820 --> 00:01:45,180
So I'll go ahead and get rid of the helper text.
20
00:01:45,180 --> 00:01:46,380
Go ahead and save that.
21
00:01:46,410 --> 00:01:50,550
So it goes back to normal and we can begin our approach for this.
22
00:01:50,550 --> 00:01:56,760
So what we're going to do is we'll come up to the top and underneath where we're creating the constants
23
00:01:56,760 --> 00:02:00,060
for our message and phone name and email.
24
00:02:00,060 --> 00:02:08,950
Let's go ahead and underneath the name will create a constant of name helper and asset name helper.
25
00:02:09,120 --> 00:02:13,850
And then we'll use state with the default of empty string.
26
00:02:13,870 --> 00:02:16,690
We can go ahead and let's copy this.
27
00:02:16,770 --> 00:02:19,680
We're going to paste that here.
28
00:02:19,680 --> 00:02:23,550
Go ahead paste it there and paste this as well.
29
00:02:23,590 --> 00:02:32,670
And now let's go and change each of these to the email helper and then the set email helper phone helper
30
00:02:33,480 --> 00:02:45,600
and the set phone helper and then the message helper and set message helper.
31
00:02:45,600 --> 00:02:53,730
So now what I'm going to do is instead of having the en change function for each of these text fields
32
00:02:53,790 --> 00:03:00,660
simply taking the event and calling the appropriate set state value for whichever value it is whether
33
00:03:00,660 --> 00:03:07,830
that's the set message cell phone or whatever set value and then it simply passes in the value.
34
00:03:07,830 --> 00:03:13,890
Well instead of just automatically passing the value into our state let's go ahead and we can actually
35
00:03:13,890 --> 00:03:18,480
define a function that we're going to run to check that value.
36
00:03:18,480 --> 00:03:25,350
And if we do that we can then determine whether or not that input is currently valid and if it is then
37
00:03:25,350 --> 00:03:31,590
we will have the regular styles but if it isn't then we can go ahead and set the error styling along
38
00:03:31,590 --> 00:03:39,120
with the help or text and then the error styles will continue to be on that input until the input changes
39
00:03:39,300 --> 00:03:46,260
to a value that is actually valid so let's scroll back up and I actually remembered.
40
00:03:46,280 --> 00:03:49,430
So we will not need a message helper.
41
00:03:49,440 --> 00:03:51,800
Go ahead and delete that or name helper.
42
00:03:51,800 --> 00:03:53,400
You can go ahead and delete that too.
43
00:03:53,460 --> 00:03:58,560
We'll actually use a little trick from the way that this is set up already so that we won't even need
44
00:03:58,560 --> 00:03:59,340
to use that.
45
00:03:59,490 --> 00:04:03,530
But for the email and phone we will need those helpers.
46
00:04:03,570 --> 00:04:10,710
But let's go underneath the message constant and let's add a constant of on change.
47
00:04:10,710 --> 00:04:15,530
And this will be called within event and will then fire an arrow function.
48
00:04:15,540 --> 00:04:22,340
And here we can let there be a local variable called error.
49
00:04:22,440 --> 00:04:24,050
Then we can put a semicolon.
50
00:04:24,330 --> 00:04:35,160
And now we're going to switch over the event dot target dot I.D. so not the target value but Target
51
00:04:35,250 --> 00:04:36,780
dot I.D..
52
00:04:36,780 --> 00:04:37,140
All right.
53
00:04:37,200 --> 00:04:38,850
And let's open this up.
54
00:04:38,850 --> 00:04:46,560
But if you come down and look where this idea is coming from the I.D. that we are setting on these texts
55
00:04:46,560 --> 00:04:47,540
fields.
56
00:04:47,610 --> 00:04:54,930
So when the event fires is fired along with the I.D. of whichever field is currently firing that change
57
00:04:54,930 --> 00:04:55,800
event.
58
00:04:55,800 --> 00:05:02,610
And so we're going to switch over the I.D. to figure out whether we're on the e-mail or the phone text
59
00:05:02,610 --> 00:05:10,530
field so we're going to switch over that I.D. and in the case that we have the I.D. of email we'll open
60
00:05:10,530 --> 00:05:19,710
this up and we'll come down and we'll first actually set the e-mail to the event dot target dot value
61
00:05:20,040 --> 00:05:25,260
because we actually want to make sure that the text is popping up on the screen before we do the validation
62
00:05:25,260 --> 00:05:26,220
for it.
63
00:05:26,220 --> 00:05:32,520
So then we can come underneath there and now we need to think about how we're actually going to validate
64
00:05:32,760 --> 00:05:33,570
this value.
65
00:05:33,570 --> 00:05:41,680
How do you make sure that you have a valid e-mail well a very easy way to do this here in Javascript
66
00:05:41,950 --> 00:05:47,440
is to use a reg x test and that is short for a regular expression.
67
00:05:47,590 --> 00:05:55,660
And that is essentially a function that defines a set of characters in a string that match a certain
68
00:05:55,660 --> 00:05:56,530
pattern.
69
00:05:56,530 --> 00:05:59,130
So this is exactly what we want to do here.
70
00:05:59,200 --> 00:06:02,950
And I've gone ahead and attached to this video.
71
00:06:03,010 --> 00:06:08,550
So make sure you go ahead and download that a regex tests Dot T X T.
72
00:06:08,710 --> 00:06:16,000
So I'm going to go ahead and open this file up and here you'll see that there is an email section email
73
00:06:16,300 --> 00:06:24,120
and a phone you see underneath each one there's just this string of random looking crazy characters.
74
00:06:24,150 --> 00:06:26,400
So go ahead and copy all of that.
75
00:06:26,400 --> 00:06:29,130
Make sure you get from slash to slash.
76
00:06:29,130 --> 00:06:33,720
Copy that for the email and then it will come into our switch statement.
77
00:06:33,780 --> 00:06:39,920
And here we are going to set error now to be equal to our regex value.
78
00:06:39,930 --> 00:06:45,450
So go ahead and paste that in now paste our whole string in dot test.
79
00:06:45,750 --> 00:06:52,020
So this is a built in function that allows us to test a regular expression on a string so we can go
80
00:06:52,020 --> 00:07:01,710
ahead and then pass in our event dot target dot value and that will go ahead and return a boolean value
81
00:07:02,040 --> 00:07:05,790
based on whether or not the event dot target dot value.
82
00:07:05,790 --> 00:07:12,450
So the text that was just input to our text field whether or not that matches this pattern.
83
00:07:12,450 --> 00:07:16,380
In this case a pattern for a valid e-mail address.
84
00:07:16,410 --> 00:07:23,190
And so we can come underneath this now and if there was an error so open this up and we'll actually
85
00:07:23,250 --> 00:07:26,580
do if there was not an error.
86
00:07:26,580 --> 00:07:30,620
So this will run if error is false.
87
00:07:30,660 --> 00:07:36,900
So if there was not an error then we'll reach this block here because remember this is going to be set
88
00:07:36,900 --> 00:07:40,350
to true if our value passes.
89
00:07:40,350 --> 00:07:45,550
So if this is a valid e-mail error will actually equal to true.
90
00:07:45,630 --> 00:07:47,820
So maybe that's actually a confusing name.
91
00:07:47,850 --> 00:07:48,650
So let's change.
92
00:07:48,660 --> 00:07:49,170
Error.
93
00:07:49,530 --> 00:07:53,360
Let's go ahead and change this to valid or less valid.
94
00:07:53,370 --> 00:07:57,180
So now let's change this change all of those.
95
00:07:57,180 --> 00:07:57,480
OK.
96
00:07:57,510 --> 00:08:04,830
And now this should make more sense because now valid will be true if this is a valid e-mail.
97
00:08:04,860 --> 00:08:12,030
This will return a value of true for this test if it meets the pattern so it will be valid.
98
00:08:12,120 --> 00:08:18,080
And this will return False if it does not meet the pattern so valid will be false.
99
00:08:18,120 --> 00:08:21,200
So if it is not valid.
100
00:08:21,240 --> 00:08:31,570
So if the value is not valid then we would want to set our e-mail helper to the text invalid e-mail.
101
00:08:31,590 --> 00:08:32,490
All right.
102
00:08:32,490 --> 00:08:40,850
And then otherwise so else well then we would go ahead and set e-mail helper to the empty string.
103
00:08:40,890 --> 00:08:47,150
And this is to make sure that once we do finally have a valid e-mail we go ahead and clear out the helper
104
00:08:47,160 --> 00:08:54,450
text because the helper text is actually going to determine whether or not the error styling is applied.
105
00:08:54,450 --> 00:09:01,470
But we need to come underneath this if block and add a break statement and then let's come underneath
106
00:09:01,530 --> 00:09:08,420
the break statement and add a default case where we also just break.
107
00:09:08,460 --> 00:09:08,800
All right.
108
00:09:08,820 --> 00:09:10,230
But we're not finished yet.
109
00:09:10,230 --> 00:09:16,800
Go ahead remove the empty line because we need to add the case for if we have the event dot target dot
110
00:09:16,800 --> 00:09:18,760
I.D. of phone.
111
00:09:18,810 --> 00:09:26,850
So if we were on the phone text field then we're going to want to set our phone to the event targets
112
00:09:27,090 --> 00:09:34,710
dot value to make sure that our text field is updated and then we will set the valid variable this time
113
00:09:34,740 --> 00:09:35,540
equal.
114
00:09:35,580 --> 00:09:43,770
We'll go over to the rejects tests to don t x t that is attached to this video and go ahead and copy
115
00:09:43,980 --> 00:09:48,990
that rejects value and they'll come over to the code we'll paste that in.
116
00:09:49,020 --> 00:09:59,070
And again we will dot test the event dot target dot value and this will now return true if we have a
117
00:09:59,070 --> 00:10:00,870
valid phone number.
118
00:10:00,870 --> 00:10:10,320
Otherwise it will return false invalid it will be false so we can then come and check if not valid then
119
00:10:10,380 --> 00:10:13,800
we will set the phone helper.
120
00:10:13,800 --> 00:10:25,100
Now two in valid phone and then otherwise else we will set the phone helper to the empty string after
121
00:10:25,100 --> 00:10:32,320
that make sure we come down and add a break statement and that switch block should be taken care of.
122
00:10:33,370 --> 00:10:43,010
But now we need to go ahead and set the on change function to our text fields right here for the phone.
123
00:10:43,030 --> 00:10:51,790
On change this is now going to be the on change function and the email will be the on change function
124
00:10:52,060 --> 00:10:52,880
as well.
125
00:10:52,900 --> 00:10:58,510
We're going to change the name and the message text fields here in just a second.
126
00:10:58,870 --> 00:11:02,560
But now on our phone and e-mail text fields.
127
00:11:02,560 --> 00:11:09,910
Here is where the cleverness of this approach comes in because we can come onto our e-mail field and
128
00:11:09,910 --> 00:11:17,860
we can add the error prop but we'll set this equal to some javascript where we'll check the email helper
129
00:11:18,010 --> 00:11:26,680
dot length and if that is not equal to zero Well then this statement will return true.
130
00:11:26,680 --> 00:11:29,580
So if this is not equal to zero.
131
00:11:29,620 --> 00:11:37,390
So if we do indeed have a length any length greater than zero on the email helper.
132
00:11:37,390 --> 00:11:44,560
And remember we're setting the email helper whenever we have a not valid email.
133
00:11:44,620 --> 00:11:49,460
It will set the invalid e-mail text instead of the empty string.
134
00:11:49,630 --> 00:11:56,260
So the empty string it does have a length of zero but once we have some error text in there whenever
135
00:11:56,260 --> 00:12:02,850
there is not a valid e-mail then that error text then has a length greater than zero.
136
00:12:02,890 --> 00:12:09,030
So we can scroll down and here when we're checking if this does not equal 0.
137
00:12:09,040 --> 00:12:16,330
So if this is any value but zero then it must mean that we have some helper text in our state which
138
00:12:16,330 --> 00:12:19,430
means that we must have an invalid email.
139
00:12:19,510 --> 00:12:21,830
So this will then return true.
140
00:12:21,940 --> 00:12:30,360
Setting the error a prop to true and giving us the error styles so we can go ahead and we can copy this
141
00:12:30,630 --> 00:12:38,160
and paste it down onto the phone section as well but make sure to change email helper to phone helper
142
00:12:38,460 --> 00:12:43,600
and we'll do the same thing making sure that that is not equal to zero.
143
00:12:43,620 --> 00:12:49,650
Let's go ahead and save this file now and we can come over to our page and you saw refresh.
144
00:12:49,650 --> 00:12:56,100
And now if we come over to the e-mail you see there's no Stiles yet but as soon as I type into this
145
00:12:56,580 --> 00:12:58,610
you see it goes red.
146
00:12:58,650 --> 00:13:01,250
It looks like our help or text is not showing up though.
147
00:13:01,260 --> 00:13:05,000
And that's because we haven't actually set the help or text props.
148
00:13:05,040 --> 00:13:13,770
So make sure on the email text field we'll set the helper text prop equal to the email helper value
149
00:13:14,100 --> 00:13:22,220
and on our phone text field we'll set the helper text equal to the phone helper value.
150
00:13:22,380 --> 00:13:25,640
So go ahead and save that page will refresh again.
151
00:13:25,680 --> 00:13:32,640
And now if I go ahead you see we don't have any styles yet but if I go ahead and type in you see immediately
152
00:13:32,700 --> 00:13:39,300
we have invalid e-mail pops up as well as the read styles on the text field and I can continue and I'll
153
00:13:39,300 --> 00:13:48,060
type in Zachary at gmail dot com and you see as soon as I have a valid e-mail the error styles goes
154
00:13:48,060 --> 00:13:55,380
away along with our helper text so we can go on the phone and let's start putting in a phone number
155
00:13:55,410 --> 00:14:01,350
you see if I type in any thing but numbers it of course is going to say it's invalid.
156
00:14:01,350 --> 00:14:10,800
But then if I come home we can do 5 5 5 5 5 5 5 5 5 5 and as soon as we have a valid phone number in
157
00:14:10,800 --> 00:14:13,350
there it then goes to being correct.
16995
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.