You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2024-10-20-some-useful-tips-and-tricks-about-form-validation-in-javascript.md
+90-81Lines changed: 90 additions & 81 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,15 @@
2
2
layout: post
3
3
title: "Some Useful Tips And Tricks About Form Validation in JavaScript"
4
4
tags: tutorial webdev javascript
5
-
image: /img/posts/yup.png
5
+
image: /img/posts/zod.png
6
6
typewriter-delay: 20
7
7
---
8
+
8
9
As time goes by, sooner or later, every developer has been tasked with building a form. π οΈ
9
10
10
11
Well, as far as I'm concerned, that's not one of the funniest things to do. π
11
12
12
-
But it turns out that even the simpler of websites out there is going to need at least a basic contact form. π€·ββοΈ
13
+
But it turns out that even the simplest of websites out there is going to need at least a basic contact form. π€·ββοΈ
13
14
14
15
And, sometimes, there are cases where you are building a really complex and big application with forms everywhere. π€
15
16
@@ -19,7 +20,7 @@ But, in this post, I'm not going to show you a specific way of building a form n
19
20
20
21
I'm going to try to be framework agnostic as well, but I might use a couple of lines of `React` here and there, as that's what I mostly use day by day.
21
22
22
-
Anyway, I promise you that this will not retain you from using what I will show you in any other framework you might happen to use or even [Vanilla JS](http://vanilla-js.com/)!
23
+
Anyway, I promise you that this will not prevent you from using what I will show you in any other framework you might happen to use or even [Vanilla JS](http://vanilla-js.com/)!
23
24
24
25
Well, at this point you may be wondering what I'm precisely going to talk about, then? π§
25
26
@@ -29,46 +30,43 @@ Well, at this point you may be wondering what I'm precisely going to talk about,
29
30
>
30
31
> -- <cite>[Wikipedia][1]</cite>
31
32
32
-
To my advice, one of the most important task we have while building a form is to ensure that the data is proactively validated.
33
+
To my advice, one of the most important tasks we have while building a form is to ensure that the data is proactively validated.
33
34
34
35
Of course, there MUST be validation on the backend as well, that's mandatory. βοΈ
35
36
36
37
Never validate data only on the front-end! This might be dangerous. β’οΈ
37
38
38
-
But, actually, the real reason why you should do that, is to preserve the mental health of your user. Really! π
39
+
But, actually, the real reason why you should do that is to preserve the mental health of your user. Really! π
39
40
40
-
As a matter of fact, as a user, I often run into long and/or complex and evil form that gives no hint to me about what I should do next or if the data that I provided is correct. π
41
+
As a matter of fact, as a user, I often run into long and/or complex and evil forms that give no hint to me about what I should do next or if the data that I provided is correct. π
41
42
42
-
These are some question that I'm sure even you had while filling-out this kind of forms:
43
+
These are some questions that I'm sure even you had while fillingout this kind of forms:
43
44
44
-
1) Is there life on mars? π½
45
-
2) Will the code I wrote yesterday compile? π€
46
-
3) But most importantly... Why the hell is the f**** submit button disabled?!? π€¬
45
+
1. Is there life on Mars? π½
46
+
2. Will the code I wrote yesterday compile? π€
47
+
3. But most importantly... Why the hell is the f\*\*\*\* submit button disabled?!? π€¬
47
48
48
-
Well, to be honest sometimes happens that the submit button is ALWAYS enabled (really?). πΆ
49
+
Well, to be honest, sometimes it happens that the submit button is ALWAYS enabled (really?). πΆ
49
50
50
51
As a developer, I strive to do my best to avoid these frustrating situations.
51
52
52
53
If only we could make the user βfail fast, fail oftenβ...
53
54
54
55
What if I told you we can accomplish this task easily?
55
56
56
-
## Yup to the rescue π
57
+
## Zod to the rescue π
58
+
59
+
> Zod is a TypeScript-first schema declaration and validation library for JavaScript and Node.js.
57
60
58
-
> Yup is a JavaScript object schema validator and object parser.
0 commit comments