-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforms-validation.html
433 lines (381 loc) · 17.2 KB
/
forms-validation.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>HTML5 Fundamentals</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="img/favicon.png">
<link rel="stylesheet" href="css/bootstrap.css" type="text/css" />
<link rel="stylesheet" href="css/site.css" type="text/css" />
<link rel="stylesheet" href="js/highlight/styles/github.css" />
<script src="js/modernizr.min.js" type="text/javascript"></script>
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/bootstrap.js" type="text/javascript"></script>
<script src="js/highlight/highlight.pack.js" type="text/javascript"></script>
<script src="js/app.js" type="text/javascript"></script>
</head>
<body>
<main>
<header>
<!-- Fixed navbar -->
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">
<img src="img/html5-fundamentals.png" title="HTML5 Fundamentals" class="hidden-sm" />
<img src="img/html5.png" title="HTML5 Fundamentals" class="visible-sm" />
</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="example.html" class="nav-link">Example</a></li>
<li class="separator visible-md visible-lg">|</li>
<li><a href="selection.html" class="nav-link">Selection</a></li>
<li>
<div class="dropdown">
<span class="nav-link" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Forms<span class="caret"></span>
</span>
<ul class="dropdown-menu" aria-labelledby="dLabel">
<li><a href="forms-markup.html">Forms</a></li>
<li><a href="forms-pseudo-classes.html">Pseudo Classes</a></li>
<li><a href="forms-validation.html">Native Validation</a></li>
<li><a href="forms-validation-custom.html">Custom Validation</a></li>
<li><a href="forms-validation-bootstrap.html">Bootstrap Module</a></li>
</ul>
</div>
</li>
<li>
<div class="dropdown">
<span class="nav-link" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Media<span class="caret"></span>
</span>
<ul class="dropdown-menu" aria-labelledby="dLabel">
<li><a href="media-basics.html">Basics</a></li>
<li><a href="media-scripted.html">Scripted Controls</a></li>
<li><a href="media-dynamic.html">Dynamic Load</a></li>
</ul>
</div>
</li>
<li>
<div class="dropdown">
<span class="nav-link" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Canvas<span class="caret"></span>
</span>
<ul class="dropdown-menu" aria-labelledby="dLabel">
<li><a href="canvas-basics.html">Basics</a></li>
<li><a href="canvas-manipulation.html">Manipulation</a></li>
<li><a href="canvas-clipping.html">Clipping</a></li>
<li><a href="canvas-charts.html">Charts</a></li>
<li><a href="canvas-video.html">Video</a></li>
</ul>
</div>
</li>
<li>
<div class="dropdown">
<span class="nav-link" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Drag & Drop<span class="caret"></span>
</span>
<ul class="dropdown-menu" aria-labelledby="dLabel">
<li><a href="dd-basics.html">Basics</a></li>
<li><a href="dd-effects.html">Effects</a></li>
<li><a href="dd-datatransfer.html">Data Transfer Types</a></li>
<li><a href="dd-files.html">Files</a></li>
</ul>
</div>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
</header>
<article id="content-container" class="container">
<div>
<!-- Nav tabs -->
<ul class="nav nav-tabs code-tabs" role="tablist">
<li role="presentation" class="active"><a href="#content-tab" aria-controls="content" role="tab" data-toggle="tab">Content</a></li>
<li role="presentation"><a href="#markup-tab" aria-controls="markup" role="tab" data-toggle="tab">Markup</a></li>
<li role="presentation"><a href="#css-tab" aria-controls="profile" role="tab" data-toggle="tab">CSS</a></li>
<li role="presentation"><a href="#javascript-tab" aria-controls="messages" role="tab" data-toggle="tab">JavaScript</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="content-tab">
<div id="example-container">
<!-- example markup -->
<form id="change-email-form">
<input type="text"
name="username"
id="username"
placeholder="username"
pattern="[a-zA-Z ]{5,}"
maxlength="30"
required
class="form-control">
<div class="validation-messages">
<span data-rule="valueMissing" class="hide">The username is required.</span>
<span data-rule="patternMismatch" class="hide">Must be a series of alpha
characters only (min 5, max 30).</span>
</div>
<input type="email"
id="email"
placeholder="email address"
title="Email address is required"
required
class="form-control">
<div class="validation-messages">
<span data-rule="valueMissing" class="hide">
An email address is required.</span>
<span data-rule="typeMismatch" class="hide">
Special characters are not allowed.</span>
</div>
<div class="push-down-top">
<button type="button" class="btn btn-link">Cancel</button>
<button type="button" id="login-button" class="btn">Login</button>
</div>
</form>
<!-- /example markup -->
</div>
</div>
<div role="tabpanel" class="tab-pane fade" id="markup-tab">
<pre><code class="html hljs"><form id="change-email-form">
<input type="text"
name="username"
id="username"
placeholder="username"
pattern="[a-zA-Z ]{5,}"
maxlength="30"
required
class="form-control">
<div class="validation-messages">
<span data-rule="valueMissing" class="hide">The username is required.</span>
<span data-rule="patternMismatch" class="hide">Must be a series of alpha
characters only (min 5, max 30).</span>
</div>
<input type="email"
id="email"
placeholder="email address"
title="Email address is required"
required
class="form-control">
<div class="validation-messages">
<span data-rule="valueMissing" class="hide">
An email address is required.</span>
<span data-rule="typeMismatch" class="hide">
Special characters are not allowed.</span>
</div>
<div class="push-down-top">
<button type="button" class="btn btn-link">Cancel</button>
<button type="button" id="login-button" class="btn">Login</button>
</div>
</form></code></pre>
</div>
<div role="tabpanel" class="tab-pane fade" id="css-tab">
<pre><code class="css hljs">input:focus:invalid,
textarea:focus:invalid,
select:focus:invalid {
color: #c33;
border-color: #c33;
}
input:focus:invalid:focus,
textarea:focus:invalid:focus,
select:focus:invalid:focus {
border-color: #c33;
-webkit-box-shadow: 0 0 6px #fcc;
-moz-box-shadow: 0 0 6px #fcc;
box-shadow: 0 0 6px #fcc;
}
input:invalid,
textarea:invalid,
select:invalid {
border-bottom: 1px solid #c33;
}
input:valid + .invalid /* Adjacent sibling selector */ {
visibility: hidden;
}
input:invalid + .invalid {
visibility: visible;
}
input:required {
border-left: 4px solid #c33;
}
input:valid {
border-bottom: transparent;
}
.invalid {
color: #999;
}</code></pre>
</div>
<div role="tabpanel" class="tab-pane fade" id="javascript-tab">
<pre><code class="javascript hljs">document.addEventListener('DOMContentLoaded', function(e) {
// Create a container for
// validation rule names.
var ruleNames = [];
var forEach = Array.prototype.forEach;
// Fills array with rule names.
// Looks for all SPANs with the data-rule
// attribute and then adds the rule
// name to the array.
var ruleElements = document.querySelectorAll('span[data-rule]');
forEach.call(ruleElements, function(element) {
var ruleName = element.getAttribute('data-rule');
if (ruleNames.indexOf(ruleNames) < 0) {
ruleNames.push(ruleName);
}
});
// First clear the UI by hiding all
// validation messages. Then run
// validation rules on the selected form.
var validate = function() {
var messages = document.querySelectorAll(".validation-messages span");
forEach.call(messages, function(message){
message.classList.add('hide')
});
document.getElementById('change-email-form').checkValidity();
};
// Check each input element to determine
// which element is invalid. Once an
// invalid state is detected, then loop
// through the validation rules to find
// out which is broken and therefore
// which message to display to the user.
var validationFail = function(e) {
var element, validity;
element = e.currentTarget;
validity = element.validity;
if (!validity.valid) {
ruleNames.forEach(function(ruleName) {
checkRule(validity,ruleName,element);
});
e.preventDefault();
}
};
// Uses the instance of the input element's
// ValidityState object to run a validation
// rule. If the validation rule returns
// 'true' then the rule is broken and
// the appropriate validation message
//is exposed to the user.
var checkRule = function(state, ruleName, element) {
if (state[ruleName]) {
var rules = element
.nextElementSibling
.querySelectorAll('[data-rule="' + ruleName + '"]');
forEach.call(rules, function(rule){
rule.classList.remove('hide');
});
}
};
// Attaches validation event handlers to
// all input elements that are NOT buttons.
var inputElements = document.querySelectorAll('input:not(button)');
forEach.call(inputElements, function(input) {
input.oninvalid = validationFail;
input.onblur = validate;
});
document.getElementById('login-button').addEventListener('click', validate, false);
});</code></pre>
</div>
<!-- -------------------------------------------------------------------- -->
<script>
document.addEventListener('DOMContentLoaded', function(e) {
// Create a container for
// validation rule names.
var ruleNames = [];
var forEach = Array.prototype.forEach;
// Fills array with rule names.
// Looks for all SPANs with the data-rule
// attribute and then adds the rule
// name to the array.
var ruleElements = document.querySelectorAll('span[data-rule]');
forEach.call(ruleElements, function(element) {
var ruleName = element.getAttribute('data-rule');
if (ruleNames.indexOf(ruleNames) < 0) {
ruleNames.push(ruleName);
}
});
// First clear the UI by hiding all
// validation messages. Then run
// validation rules on the selected form.
var validate = function() {
var messages = document.querySelectorAll('.validation-messages span');
forEach.call(messages, function(message){
message.classList.add('hide')
});
document.getElementById('change-email-form').checkValidity();
};
// Check each input element to determine
// which element is invalid. Once an
// invalid state is detected, then loop
// through the validation rules to find
// out which is broken and therefore
// which message to display to the user.
var validationFail = function (e) {
var element, validity;
element = e.currentTarget;
validity = element.validity;
if (!validity.valid) {
ruleNames.forEach(function (ruleName) {
checkRule(validity, ruleName, element);
});
e.preventDefault();
}
};
// Uses the instance of the input element's
// ValidityState object to run a validation
// rule. If the validation rule returns
// 'true' then the rule is broken and
// the appropriate validation message
//is exposed to the user.
var checkRule = function(state, ruleName, element) {
if (state[ruleName]) {
var rules = element
.nextElementSibling
.querySelectorAll('[data-rule="' + ruleName + '"]');
forEach.call(rules, function(rule){
rule.classList.remove('hide');
});
}
};
// Attaches validation event handlers to
// all input elements that are NOT buttons.
var inputElements = document.querySelectorAll('input:not(button)');
forEach.call(inputElements, function(input) {
input.oninvalid = validationFail;
input.onblur = validate;
});
document.getElementById('login-button').addEventListener('click', validate, false);
});
</script>
</div>
</div>
</article>
<footer>
<div class="container">
<div class="row push-down-top-lg hidden-xs">
<div class="col-xs-offset-2 col-xs-3">
<img src="img/html5-fundamentals.png" title="HTML5 Fundamentals" />
<p>by <a href="http://craigshoemaker.net">Craig Shoemaker</a></p>
<p class="lead"><a href="http://twitter.com/craigshoemaker">Craig Shoemaker</a> is a <a href="http://www.infragistics.com">developer</a>,
<a href="http://www.pluralsight.com/author/craig-shoemaker">instructor</a>, <a href="http://code-magazine.com/SearchResults.aspx?search=craig%20shoemaker">writer</a>, <a href="http://polymorphicpodcast.com">podcaster</a>, and a man of many hats at <a href="http://www.infragistics.com">Infragistics</a>.</p>
<div class="push-down-top-md">
<a href="https://www.pluralsight.com/courses/html5-fundamentals"><img src="img/pluralsight.png" title="HTML5 Fundamentals" /></a>
</div>
</div>
<div class="col-xs-offset-1 col-xs-3">
<a href="http://craigshoemaker.net"><img src="img/craig-shoemaker.png" class="img-responsive" title="Craig Shoemaker" /></a>
</div>
</div>
</div>
</footer>
</main>
</body>
</html>