-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
417 lines (412 loc) · 20.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<!doctype html>
<html lang="en" data-bs-theme="dark">
<head>
<title>Easy Boostrap 5 Form Validation</title>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<link rel="stylesheet" href="example/bootstrap5.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/hybrid.min.css">
</head>
<body>
<header class="py-3 bg-primary-subtle fixed-top">
<div class="container d-flex justify-content-center">
<ul class="nav nav-pills">
<li class="nav-item"><a href="https://github.com/myJW3B/Easy-Bootstrap-5-Form-Validation" class="nav-link">
<i class="bi bi-github"></i> <span class="d-none d-lg-inline-block">Github</span></a></li>
<li class="nav-item"><a href="#example" class="nav-link">Example</a></li>
<li class="nav-item"><a href="#simple-example" class="nav-link">Simple Example</a></li>
<li class="nav-item"><a href="#full-code" class="nav-link">Full Code</a></li>
<li class="nav-item"><a href="#guide" class="nav-link">Guide</a></li>
<li class="nav-item"><a href="#faq" class="nav-link">FAQs</a></li>
<li class="nav-item"><a href="#about" class="nav-link">About</a></li>
</ul>
</div>
</header>
<main
data-bs-spy="scroll" data-bs-target="#navbar-example2"
data-bs-root-margin="50px 0px -40%"
data-bs-smooth-scroll="true"
class="scrollspy-example bg-body-tertiary mt-5"
tabindex="0">
<section id="example" class="container">
<div class="row">
<div class="mx-auto col-12 col-md-10 col-lg-8 pt-5">
<h3>Example</h3>
<div class="mb-3 p-3 bg-dark-subtle border border-secondary-subtle rounded">
<ol>
<li>Fill out the form below.</li>
<li>Display Name length needs to be between 3-30 characters.</li>
<li>Email has to be correct format.</li>
<li>Passwords have to match.</li>
<li>Uses ajax to check ifbinput value can be used.<br>
Use cases:
<ul>
<li>Check if email is already found.</li>
<li>Check if post name is already found.</li>
<li>ect...</li>
</ul>
</li>
</ol>
</div>
<form role="form" id="register-form" action="javascript:void(0);" method="POST"
class="jw3b-form border rounded bg-dark-subtle p-4 p-md-3 border-primary-subtle">
<h4 class="border-bottom border-primary mb-3">Register Below</h4>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-person-fill"></i></span>
<div class="floating-label">
<input id="mf-rname" name="mf-rname" value="" class="mf-valid form-control" required
placeholder="Display Name" minlength="3" maxlength="30"
data-error-msg="Display name has to be at between 3-30 characters long" type="text">
<label for="mf-rname">Display Name</label>
</div>
</div>
<div class="form-text mb-3" id="mf-rname-valid"></div>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-envelope-fill"></i></span>
<div class="floating-label">
<input id="mf-remail" name="mf-remail" value="" class="mf-valid form-control" required type="email"
placeholder="[email protected]" data-error-msg="Please enter a valid email address."
data-mf-ajax-check="email" data-mf-ajax-url="email.json">
<label for="mf-remail">[email protected]</label>
</div>
</div>
<div class="form-text mb-3" id="mf-remail-valid"></div>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-key-fill"></i></span>
<div class="floating-label">
<input id="mf-rpass" name="mf-rpass" value="" class="mf-valid form-control" required
placeholder="Password" type="password" minlength="6" data-mf-same-as="#mf-rpass2"
data-error-msg="Min 6 characters or passwords are not the same.">
<label for="mf-rpass">Password</label>
</div>
</div>
<div class="form-text mb-3" id="mf-rpass-valid"></div>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-key-fill"></i></span>
<div class="floating-label ">
<input id="mf-rpass2" name="mf-rpass2" value="" class="mf-valid form-control" required
placeholder="Confirm Password" type="password" minlength="6" data-mf-same-as="#mf-rpass"
data-error-msg="Min 6 characters or passwords are not the same.">
<label for="mf-rpass2">Confirm Password</label>
</div>
</div>
<div class="form-text mb-3" id="mf-rpass2-valid"></div>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-globe-americas"></i></span>
<span class="input-group-text">https://</span>
<div class="floating-label ">
<input id="mf-rweb" name="mf-rweb" value="" class="mf-valid form-control" required placeholder="Website"
minlength="4" data-mf-ajax-url="example/website-url.json" data-mf-ajax-check="website-url"
data-error-msg="4 characters long min." type="text">
<label for="mf-rweb">Website</label>
</div>
<span class="input-group-text">.exam.ple</span>
</div>
<div class="form-text" id="mf-rweb-valid"></div>
<div class="form-text mb-3">Examples are cool</div>
<div class=" checkbox mb-3">
<label for="mf-rterms">
<input id="mf-rterms" name="mf-rterms" value="1" type="checkbox">
By clicking Register, you agree to the terms of use.
</label>
</div>
<div id="register-form-results"></div>
<input type="hidden" name="form_id" value="register-form" id="form-id-register-form">
<hr>
<div class="text-center clearfix">
<button id="register-form-btn" class="w-100 btn btn-lg btn-primary" type="submit">
<i class="bi bi-check-lg"></i> Register
</button>
</div>
</form>
</div>
</div>
</section>
<hr>
<section id="simple-example" class="container">
<div class="row">
<div class="mx-auto col-12 col-md-10 col-lg-8">
<h3>Simple Example</h3>
<div class="mb-3 p-3 bg-dark-subtle border border-primary-subtle rounded">
<label for="example" class="form-label">Username</label>
<input type="text" class="form-control mf-valid" id="example" placeholder="Type A Name" minlength="4"
maxlength="30" pattern="[a-z]{4,30}" required
data-error-msg="Username must be lowercase between 4-30 characters long.">
<div class="form-text mb-3" id="example-valid"></div>
</div>
<h3>Code</h3>
<div class="mb-3 p-3 bg-dark-subtle border border-primary-subtle rounded">
<code><form class="<span class="text-warning fw-bold">jw3b-form</span>"><br>
<div class="</code>mb-3<code>"><br>
<label for="</code>example<code>" class="</code>form-label<code>"></code><span class="text-white-50">Username</span><code></label><br>
<input type="</code>text<code>" class="</code>form-control <span class="text-warning fw-bold">mf-valid</span><code>" id="</code><span class="text-info">example</span><code>" <br>
placeholder="</code>Type A Name<code>" required</code><br>
<code class="text-success-emphasis">minlength="</code><span class="text-warning">4</span><code class="text-success-emphasis">" maxlength="</code><span class="text-warning">30</span><code class="text-success-emphasis">" pattern="</code><span class="text-warning">[a-z]{4,30}</span><code class="text-success-emphasis">"<br>
data-error-msg="</code><span class="text-warning-emphasis">Username must be lowercase between 4-30 characters long.</span><code class="text-success-emphasis">"></code><br>
<code><div class="</code>form-text mb-3<code>" id="</code><span class="text-info">example</span>-valid<code>"></div><br>
</div><br>
<form>
</code>
</div>
<div class="mb-3 p-3 bg-dark-subtle border border-secondary-subtle rounded">
<p>Notice the html attributes <code>minlength</code>, <code>maxlength</code> and <code>pattern</code>? That literally does the trick here for validating.</p>
<p>Ensure the input element has an id that matches the beginning of the <code>form-text</code> div after the element, with <code>-valid</code> added to that div. This is the element that will display the error message when the form element does not validate.</p>
</div>
</div>
</div>
</section>
<hr>
<section id="full-code" class="container">
<div class="row">
<div class="mx-auto col-12 col-md-10 col-lg-8">
<h3>Full Code</h3>
<pre class="mb-3 p-3 bg-dark-subtle border border-primary-subtle rounded">
<code class="language-html"><form role="form" id="register-form" action="javascript:void(0);" method="post"
class="jw3b-form border rounded bg-dark-subtle p-4 p-md-3 border-primary-subtle">
<h4 class="border-bottom border-primary mb-3">Register Below</h4>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-person-fill"></i></span>
<div class="floating-label">
<input id="mf-rname" name="mf-rname" value="" class="mf-valid form-control" required
placeholder="Display Name" minlength="3" maxlength="30"
data-error-msg="Display name has to be at between 3-30 characters long" type="text">
<label for="mf-rname">Display Name</label>
</div>
</div>
<div class="form-text mb-3" id="mf-rname-valid"></div>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-envelope-fill"></i></span>
<div class="floating-label">
<input id="mf-remail" name="mf-remail" value="" class="mf-valid form-control" required type="email"
placeholder="[email protected]" data-error-msg="Please enter a valid email address."
data-mf-ajax-check="email" data-mf-ajax-url="email.json">
<label for="mf-remail">[email protected]</label>
</div>
</div>
<div class="form-text mb-3" id="mf-remail-valid"></div>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-key-fill"></i></span>
<div class="floating-label">
<input id="mf-rpass" name="mf-rpass" value="" class="mf-valid form-control" required
placeholder="Password" type="password" minlength="6" data-mf-same-as="#mf-rpass2"
data-error-msg="Min 6 characters or passwords are not the same.">
<label for="mf-rpass">Password</label>
</div>
</div>
<div class="form-text mb-3" id="mf-rpass-valid"></div>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-key-fill"></i></span>
<div class="floating-label ">
<input id="mf-rpass2" name="mf-rpass2" value="" class="mf-valid form-control" required
placeholder="Confirm Password" type="password" minlength="6" data-mf-same-as="#mf-rpass"
data-error-msg="Min 6 characters or passwords are not the same.">
<label for="mf-rpass2">Confirm Password</label>
</div>
</div>
<div class="form-text mb-3" id="mf-rpass2-valid"></div>
<div class="input-group">
<span class="input-group-text"><i class="bi bi-globe-americas"></i></span>
<span class="input-group-text">https://</span>
<div class="floating-label ">
<input id="mf-rweb" name="mf-rweb" value="" class="mf-valid form-control" required placeholder="Website"
minlength="4" data-mf-ajax-url="website-url.json" data-mf-ajax-check="website-url"
data-error-msg="4 characters long min." type="text">
<label for="mf-rweb">Website</label>
</div>
<span class="input-group-text">.exam.ple</span>
</div>
<div class="form-text" id="mf-rweb-valid"></div>
<div class="form-text mb-3">Examples are cool</div>
<div class=" checkbox mb-3">
<label for="mf-rterms">
<input id="mf-rterms" name="mf-rterms" value="1" type="checkbox">
By clicking Register, you agree to the terms of use.
</label>
</div>
<div id="register-form-results"></div>
<input type="hidden" name="form_id" value="register-form" id="form-id-register-form">
<hr>
<div class="text-center clearfix">
<button id="register-form-btn" class="w-100 btn btn-lg btn-primary" type="submit">
<i class="bi bi-check-lg"></i> Register
</button>
</div>
</form>
</code>
</pre>
<hr>
<section id="guide" class="container">
<div class="row">
<div class="mx-auto col-12 col-lg-10">
<h3>How to use</h3>
<div class="mb-3 p-3 bg-dark-subtle border border-secondary-subtle rounded">
<p>Add the javascript file to your page.</p>
<p><code><script src="mf-valid.js"></script></code></p>
<p>Add <code>required</code> to your form elements.</p>
<p>Add <code>mf-valid</code> class to your form elements class you want the validator to check.</p>
</div>
<h4>Add <code>data-*</code> to the form elements</h4>
<table class="table">
<thead>
<tr>
<th scope="col">Attribute</th>
<th scope="col">Required</th>
<th scope="col">Example Value</th>
<th scope="col">Explain Value</th>
</tr>
</thead>
<tbody>
<tr>
<td style="white-space:pre;"><code>data-error-msg</code></td>
<td>no</td>
<td><code>Input is not correct, try again</code></td>
<td>The value will be the error message shown under the element that is invalid.</td>
</tr>
<tr>
<td style="white-space:pre;"><code>data-mf-ajax-url</code></td>
<td>no</td>
<td><code>/ajax.php</code></td>
<td>The URL to send the post data and check if the value of this element is useable or of correct
backend format.</td>
</tr>
<tr>
<td style="white-space:pre;"><code>data-mf-ajax-check</code></td>
<td>yes, if <code>data-mf-ajax-url</code> is found, this attribute must be found also.</td>
<td><code>email</code></td>
<td>This is the key to check against in your backend script.<br><code>email=${input.value}</code> would be sent to the server via a fetch post.</td>
</tr>
<tr>
<td style="white-space:pre;"><code>data-mf-same-as</code></td>
<td>no</td>
<td><code>#password2</code></td>
<td>Example usage is to ensure 2 password fields have the same value.</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<hr>
<section id="faq" class="container mb-5">
<div class="row">
<div class="mx-auto col-12 col-md-10 col-lg-8">
<h3>Frequently Asked Questions</h3>
<div class="accordion" id="accordionPanelsStayOpenExample">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button" type="button" data-bs-toggle="collapse"
data-bs-target="#panelsStayOpen-collapseOne" aria-expanded="true"
aria-controls="panelsStayOpen-collapseOne">
Is JQuery Used?
</button>
</h2>
<div id="panelsStayOpen-collapseOne" class="accordion-collapse collapse show">
<div class="accordion-body">
<strong>NO!</strong>
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
data-bs-target="#panelsStayOpen-collapseTwo" aria-expanded="false"
aria-controls="panelsStayOpen-collapseTwo">
Can I use in my projects?
</button>
</h2>
<div id="panelsStayOpen-collapseTwo" class="accordion-collapse collapse">
<div class="accordion-body">
<strong>YES!</strong> MIT License, do as you want, just dont hold me liable for any misuse you may
come
across. Its your job as a developer to ensure no security issues may arise.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
data-bs-target="#panelsStayOpen-collapseThree" aria-expanded="false"
aria-controls="panelsStayOpen-collapseThree">
Does this work fro <select> and <textarea> tags?
</button>
</h2>
<div id="panelsStayOpen-collapseThree" class="accordion-collapse collapse">
<div class="accordion-body">
<p>I did some testing with <code><select></code> tags, but was unable to get the validation to
pop
up in the native browser validation. I do plan on tweaking this more to ensure it does work with
select
tags as well.</p>
<p>I have not done any testing with <code><textarea></code> tags, but I'm sure if you added the
maxlength or min length to the <code><textarea></code> tag it will work.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
data-bs-target="#panelsStayOpen-collapseFour" aria-expanded="false"
aria-controls="panelsStayOpen-collapseFour">
Why not use <code>document.createElement()</code> for the notification?
</button>
</h2>
<div id="panelsStayOpen-collapseFour" class="accordion-collapse collapse">
<div class="accordion-body">
Because if you look at the top example, I'm using input-groups and custom floating labels, so fimding the correct parent element would be complicated depending on the setup. So I opt'd for just putting the <code><div></code> with the <code>mb-3</code> where the error notice should go.
</div>
</div>
</div>
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse"
data-bs-target="#panelsStayOpen-collapseFive" aria-expanded="false"
aria-controls="panelsStayOpen-collapseFive">
Your floating labels are different than what Bootstrap offers, how / why?
</button>
</h2>
<div id="panelsStayOpen-collapseFive" class="accordion-collapse collapse">
<div class="accordion-body">
Because I found an awesome <a href="https://github.com/tkrotoff/bootstrap-floating-label">tkrotoff/bootstrap-floating-label</a> plugin and I prefer this technique way more than bootstrap's native floating labels. Thank you <a href="https://github.com/tkrotoff">tkrotoff</a> for sharing your project!
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<hr>
<section id="about" class="container">
<div class="row">
<div class="mx-auto col-12 col-md-10 col-lg-8">
<h3>About Me.</h3>
<div class="mb-3 p-3 bg-dark-subtle border border-primary-subtle rounded">
<p>Sup, Yohn here, I wanted a simple way to validate my forms using <code>data-*</code> attributes, and everything I've found online had so much extra markup, and unneeded code, that truly is amazing for what they are and what they can accomplish, but way to bloated for what I was looking for.</p>
<p>So I spent a few hours playing around with native browser validation and came up with this approach to validate forms. Yes, it could have been wrapped a class, and made easier, but really, its not that much code to go along with it, so I figured this would be the best way to have it.</p>
<p>I also included some ajax / fetch functions that I use for submiting forms, works with multiple uploads and normal forms, or if you just wanted to do some quick ajax checks (like check if email has been used before in signup process).</p>
</div>
</div>
</div>
</section>
<div class="m-5 p-5"></div>
</main>
<footer class="p-3">
<div class="container">
<div class="row">
<div class="col-12 text-center pb-5" id="devex">
</div>
</div>
</div>
</footer>
<!-- Bootstrap JavaScript Libraries -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<script src="example/mf-valid.js"></script>
<script>
hljs.highlightAll();
</script>
</body>
</html>