Validating Input

Back to Overview
00:00 --> 00:18
Validating input is also super important. Now you start filling out a form and you do stuff, and sometimes you just type something that is not correct. You mistype your email address, you enter some ID and it's not long enough.

00:18 --> 00:26
your form should be able to say like, hey, this is likely wrong input.

00:28 --> 00:29
Please correct that.

00:29 --> 00:31
And that's what validation is for.

00:32 --> 00:34
And there are a couple of ways to do that.

00:34 --> 00:41
What this page shows is the built-in browser validation, and that's quite okay.

00:42 --> 00:45
But there are better ways to do that.

00:45 --> 00:50
For example, one of the things is, well, I just show it.

00:52 --> 01:01
Why talk in the abstract when I can talk in the concrete, I guess.

01:02 --> 01:04
Don't talk in concrete, kids.

01:04 --> 01:12
Okay, so if I press submit here, it should show, please fill out this field, which is great.

01:13 --> 01:20
But it is also like a thing that comes from the browser and then it just disappears at some point.

01:22 --> 01:25
So I press submit again.

01:25 --> 01:28
Please fill out this field and it goes away after a couple of seconds.

01:29 --> 01:32
Now, if you have a small form like this, probably OK.

01:33 --> 01:36
If you have something longer, people might just not see it.

01:36 --> 01:41
And you can do that by adding required to the input.

01:41 --> 01:49
And that will also in addition announce that this input field is required, which is a good thing.

01:49 --> 01:58
And you should do that if I put in my name or, well, that's not my name, but if I put in anything

01:58 --> 02:06
and press submit, you know, it does actually submit it. And then we get back to the tutorial.

02:06 --> 02:26
So, yeah, so you can use those to validate common things. So here we have a couple of different inputs and that might also be interesting for you.

02:26 --> 02:31
So as you can see, there are different things and this is all normal HTML input.

02:32 --> 02:35
Um, so for example, here we have an email input field.

02:35 --> 02:43
If I do, uh, fake, Hey, if, which is not a real thing and then press submit, it will

02:43 --> 02:45
say, please include an ad in the email address.

02:46 --> 02:47
This is missing an ad.

02:47 --> 02:52
If I put an ad at the end, I will press submit, please end up part following the ad.

02:52 --> 02:57
So it is even like intelligent enough to do that.

02:57 --> 03:10
But then if we add more text there, it will just submit because that's how the email validation here works.

03:11 --> 03:14
Oh, they are not marked as required.

03:14 --> 03:16
That's why it's reloading.

03:16 --> 03:18
So that's how email works.

03:19 --> 03:23
So I will pretend we have an email.

03:23 --> 03:29
Now for website, if I go and put in Yatil.net, it's a website, right?

03:29 --> 03:32
If I press submit, no, I have to enter a URL.

03:32 --> 03:36
And that's like where you see like, oh, this is like a terrible error description.

03:37 --> 03:46
Because what they really want me to do is to put in the HTTP colon double slash before the Yatil.net.

03:46 --> 03:48
And then this would work.

03:48 --> 03:54
And we have number, and of course if you put in like an A, now I press A on the keyboard

03:54 --> 03:56
and it does nothing.

03:56 --> 04:02
So this is how it's supposed to be.

04:02 --> 04:09
So you can't submit something that's not a number, you would think.

04:09 --> 04:16
And you get these like little buttons, these are actually in ten increments.

04:16 --> 04:35
And I think if I put in five, it should complain about that maybe? Yes. So please enter a valid value. The two nearest valid values are zero and ten. So if I go six and press submit, yeah, it also does zero and ten as the nearest value.

04:35 --> 04:49
So you get this information because I've constrained this to those things and I get the answer. Is this message, how long it is there, enough to read it? Probably not.

04:53 --> 05:04
Yeah, but I can also put other stuff in here like E, which is, you know, the Euler's Zahl, the Euler number, I guess.

05:05 --> 05:08
I can also put probably pi in here.

05:09 --> 05:12
I don't know how I would do that off the top of my head.

05:13 --> 05:18
But yeah, you can also put non-number numbers in here.

05:18 --> 05:23
But this is constrained to these numbers.

05:23 --> 05:31
that I set like every 10 things. Then we have range here, we have the slider and

05:31 --> 05:36
that works also with the keyboard so this is left and right arrow key up and

05:36 --> 05:46
down arrow key which works nice. Don't think there's constraint on that. Nope,

05:46 --> 05:56
You just do it and then you're back here. We have a date. So this is the date input. So this makes,

05:57 --> 06:04
just make sure that you can't enter something that's not a date at all. So, but I can still say

06:05 --> 06:19
four, five, six, 12, 12, 12, 12, stuff like that. And there is even a calendar and in some browsers,

06:19 --> 06:25
those calendars are accessible and others they are not. Here you can really nicely go through

06:26 --> 06:35
this chromium so that works well. You can have like 10, 30 here and you can even have like this

06:43 --> 06:48
time selector. My brain was like date selector, date selector, date selector, but it's a time

06:48 --> 06:59
selector. So super useful. And that prevents basically users from making bad decisions.

06:59 --> 07:04
So you want to do that. You always have to verify it on the server side as well,

07:05 --> 07:13
because I can go into the HTML and just change anything. Like this now says 1935 and I

07:14 --> 07:28
I couldn't change that, but if I go into here and into there, and then I can go basically into this.

07:32 --> 07:39
And can then say value test and press return.

07:39 --> 07:45
And then the test value would be sent to the server.

07:45 --> 07:52
Like if I remove input time and say text, then it will be revealed.

07:52 --> 07:54
Should have been.

07:54 --> 07:56
Oh, that counts as changed, I guess.

07:57 --> 08:01
So yeah, but you can do manipulations like that.

08:01 --> 08:09
And so you always have to also test it on the back end and then throw errors to the user if something is wrong.

08:22 --> 08:23
Yeah.

08:23 --> 08:23
Oh, yeah.

08:23 --> 08:27
And this is like a super, super fun little example.

08:28 --> 08:33
So in this case, this is about German license plate numbers.

08:34 --> 08:48
And you can have these patterns, which are regular expressions, which if you don't know what that is, regular expressions is basically a syntax to say like, okay, I want to search for something.

08:50 --> 08:55
I define a pattern and then I can search things in that pattern.

08:55 --> 09:02
So in this case, it says like from A to Z is allowed and then Ö, E and Ü.

09:02 --> 09:06
So the umlauts for A, O and U.

09:06 --> 09:09
And between one and three times.

09:09 --> 09:11
So that's basically what this means.

09:11 --> 09:12
And then there must be a space.

09:13 --> 09:17
And then we have A to Z for two to four times.

09:17 --> 09:20
So one time A to Z is not enough.

09:21 --> 09:23
And then we have zero to nine.

09:24 --> 09:28
So number between one and four times.

09:28 --> 09:37
Theoretically, you could say, oh, and optionally, there's an E at the end, because that would be for electro cars.

09:37 --> 09:38
They would have that.

09:39 --> 09:42
But so you can actually validate stuff like that.

09:42 --> 10:04
So you can say, so if I have, let's say, PSAB123, this would be a valid thing because it has two here, two here, and then three numbers here.

10:04 --> 10:08
But if I go 56, it says, please match the requested format.

10:08 --> 10:12
Now, this doesn't tell you what the requested format is.

10:12 --> 10:13
You have to know that.

10:14 --> 10:28
So for the city, I could do like, if I do like Cologne, and I don't know what the city thing is, you could Cologne, Cologne, which is obviously for, which is not allowed.

10:29 --> 10:31
So we'll also complain.

10:32 --> 10:36
But if you go CG, that will work.

10:36 --> 10:41
CG is not the number plate of Cologne, that's just a K. I know that.

10:44 --> 10:50
Yeah, but that's a fun thing where you can be like, oh, how is this supposed to be working?

10:50 --> 10:54
And then you can at least give your users a little bit more information.

10:54 --> 10:57
Now, you should always be forgiving to different input formats.

10:57 --> 11:10
So, for example, if you have a credit card number input, you might theoretically have like 12 numbers and you want to have them in four packs with a space in between.

11:10 --> 11:18
But maybe a user wants to put dashes in between or wants to just write them out in one line, then they should be able to do that.

11:19 --> 11:20
Same goes for phone numbers.

11:20 --> 11:24
Don't say like, oh, you have to put brackets here and spaces there.

11:25 --> 11:28
That's all like, that's not useful for you.

11:28 --> 11:31
Well, it might be useful for you, but it's not useful for your user.

11:32 --> 11:36
And you can always on the backend reformat it when you output it.

11:36 --> 11:42
So don't, you know, don't do that.

11:46 --> 11:51
Yeah, and in general, you want to do the client-side validation, which is these like,

11:51 --> 11:58
hey, you know, change your format and enter the right thing, something like that, because it's

11:59 --> 12:06
immediate and it gives you the immediate feedback. And we will talk about ARIA Live

12:06 --> 12:13
next week and that will have a lot of like other things, how you can get this immediate feedback to

12:13 --> 12:23
users. And of course, you will also want users to check their input. So if you can delete something,

12:23 --> 12:31
you want to have a, do you really want to delete this? You know, something like that. You want to

12:31 --> 12:37
give undo functionality. That's one way to address that as well, is to allow just deletion,

12:37 --> 12:41
but then you have like a trash can where you can claw stuff back if you need to.

12:44 --> 12:50
And, and yeah, and these are like important principles just to make sure that users don't

12:50 --> 12:56
do anything that is that is bad for them. And especially if it's destructive, like deleting

12:56 --> 13:03
something, you always want, you know, to have a net and, and make sure that it doesn't happen just,

13:04 --> 13:09
you know, in a click and then the user is like sad, they have to phone you and then you go like,

13:09 --> 13:14
I can't do anything because I don't have any backups for this or something like that. You

13:14 --> 13:21
know, you don't want that to happen. So you always want to confirm with the user that this is really

13:21 --> 13:23
the action they want to do.

13:23 --> 13:24
Thank you.