Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,120 --> 00:00:03,940
So in this input here,
2
00:00:03,940 --> 00:00:06,340
we might want to add some validation
3
00:00:06,340 --> 00:00:07,470
so that, for example,
4
00:00:07,470 --> 00:00:11,670
I can't submit this form when the input is empty.
5
00:00:11,670 --> 00:00:14,380
Because then I would get two empty logs here,
6
00:00:14,380 --> 00:00:17,630
and whilst we only have logs here, in most cases,
7
00:00:17,630 --> 00:00:20,410
we would probably send that data to a server
8
00:00:20,410 --> 00:00:24,230
and we don't wanna send empty data to the server.
9
00:00:24,230 --> 00:00:28,910
Though I also already wanna say the client-side validation,
10
00:00:28,910 --> 00:00:33,100
which we're doing here, where we validate in the browser,
11
00:00:33,100 --> 00:00:35,400
isn't everything you should be doing.
12
00:00:35,400 --> 00:00:38,480
If you have a real website, a real web application
13
00:00:38,480 --> 00:00:41,840
which you're building, then validating in the browser
14
00:00:41,840 --> 00:00:44,850
as we are about to add it here is great
15
00:00:44,850 --> 00:00:46,900
for the user experience
16
00:00:46,900 --> 00:00:50,020
because you give the user a direct feedback,
17
00:00:50,020 --> 00:00:53,390
but you also always should validates the input
18
00:00:53,390 --> 00:00:58,390
on the server, because this code here on the browser,
19
00:00:58,620 --> 00:01:01,700
this can all be edited by your users.
20
00:01:01,700 --> 00:01:03,610
I can dive into the source code
21
00:01:03,610 --> 00:01:06,530
and changed this raw JavaScript code.
22
00:01:06,530 --> 00:01:08,380
So this is not reliable.
23
00:01:08,380 --> 00:01:11,900
It's just there for providing a good user experience.
24
00:01:11,900 --> 00:01:15,470
It's not a security mechanism or anything like that.
25
00:01:15,470 --> 00:01:17,870
And I have an article and video on that,
26
00:01:17,870 --> 00:01:19,160
which you'll find attached,
27
00:01:19,160 --> 00:01:22,810
which also might be helpful when it comes to JavaScript
28
00:01:22,810 --> 00:01:26,370
in the browser and the security implications
29
00:01:26,370 --> 00:01:30,010
of doing things in JavaScript in the browser.
30
00:01:30,010 --> 00:01:31,500
But that's a side note.
31
00:01:31,500 --> 00:01:34,230
Let's come back to how we could validate this.
32
00:01:34,230 --> 00:01:38,060
How could we make sure that we, for example, show an error
33
00:01:38,060 --> 00:01:41,110
and don't log the values to the console
34
00:01:41,110 --> 00:01:44,360
if we try to submit an empty field?
35
00:01:44,360 --> 00:01:47,910
Well, of course, we can go to the formSubmissionHandler,
36
00:01:47,910 --> 00:01:50,150
and there we can add an if-check.
37
00:01:50,150 --> 00:01:54,860
Here we can add an if-check and we can then simply check
38
00:01:54,860 --> 00:01:56,893
if the enteredName,
39
00:01:57,930 --> 00:02:01,170
and here I'll go with that state value now,
40
00:02:01,170 --> 00:02:05,623
if that enteredName is empty maybe.
41
00:02:06,600 --> 00:02:10,150
We also might want to call trim on that enteredName string
42
00:02:10,150 --> 00:02:12,250
to remove any excess white space
43
00:02:12,250 --> 00:02:14,000
at the beginning and end.
44
00:02:14,000 --> 00:02:17,220
Now, of course, the exact validation logic you need
45
00:02:17,220 --> 00:02:21,140
depends on the form input value you are expecting.
46
00:02:21,140 --> 00:02:23,470
If it's just some name, you might be fine
47
00:02:23,470 --> 00:02:25,890
with just checking that it's not empty;
48
00:02:25,890 --> 00:02:27,090
for an email address,
49
00:02:27,090 --> 00:02:30,540
you might wanna validate that it's a valid email.
50
00:02:30,540 --> 00:02:31,860
But I wanna keep it simple here,
51
00:02:31,860 --> 00:02:34,700
so I'm just checking for it being empty or not.
52
00:02:34,700 --> 00:02:38,620
Now, if it is empty, if we have an empty string here,
53
00:02:38,620 --> 00:02:42,820
then I don't want to continue with the next lines
54
00:02:42,820 --> 00:02:43,910
of code.
55
00:02:43,910 --> 00:02:47,320
Because here we are using the entered value,
56
00:02:47,320 --> 00:02:51,820
and I don't want to do that if that value is empty.
57
00:02:51,820 --> 00:02:54,400
Hence here, I wanna return,
58
00:02:54,400 --> 00:02:56,920
which returns from this overall function
59
00:02:56,920 --> 00:02:59,930
and therefore cancels the function execution.
60
00:02:59,930 --> 00:03:03,833
Now, the rest of this function will not be executed.
61
00:03:04,860 --> 00:03:09,300
So if we saved this and I reload to clear my log,
62
00:03:09,300 --> 00:03:13,270
if I now hit Submit here, you see nothing gets added here,
63
00:03:13,270 --> 00:03:15,300
not even empty console logs.
64
00:03:15,300 --> 00:03:16,750
There's nothing here.
65
00:03:16,750 --> 00:03:20,170
If I do enter something, though, we still see that,
66
00:03:20,170 --> 00:03:22,130
so that's working.
67
00:03:22,130 --> 00:03:24,230
But what we're not doing is
68
00:03:24,230 --> 00:03:27,070
we're not showing the user feedback.
69
00:03:27,070 --> 00:03:30,140
We're not showing the user that something went wrong.
70
00:03:30,140 --> 00:03:33,000
Of course, users don't have the developer tools open.
71
00:03:33,000 --> 00:03:37,150
So if I'm a user visiting this page and I click Submit,
72
00:03:37,150 --> 00:03:39,480
I don't know if anything happened
73
00:03:39,480 --> 00:03:41,930
or if my input here is invalid.
74
00:03:41,930 --> 00:03:44,870
I might be able to guess that an empty input is invalid,
75
00:03:44,870 --> 00:03:47,400
but I get no feedback from the app.
76
00:03:47,400 --> 00:03:51,990
So that's why we might want to show some error feedback.
77
00:03:51,990 --> 00:03:54,800
And we can, of course, implement such a functionality
78
00:03:54,800 --> 00:03:56,083
with React as well.
6243
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.