-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.html
372 lines (329 loc) · 19.6 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
<!doctype html>
<html lang="en" dir="ltr" itemscope itemtype="http://schema.org/Article" data-debug-console="false">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>HTML5 Form Shim</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
<link rel="shortcut icon" href="./assets/favicon.ico" />
<link rel="icon" href="./assets/favicon.ico" type="image/ico">
<link href="./assets/demo.css" rel="stylesheet" media="all" />
<link rel="publisher" href="https://plus.google.com/113857365337532405020" />
<link href='http://fonts.googleapis.com/css?family=Berkshire+Swash' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:600,700' rel='stylesheet' type='text/css'>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="./build/jquery.html5form.js" type="text/javascript"></script>
<!-- Custom input[type=color] widget example -->
<link rel="stylesheet" media="screen" type="text/css" href="http://demo.dsheiko.com/html5formshim/vendors/colorpicker/css/colorpicker.css" />
<script type="text/javascript" src="http://demo.dsheiko.com/html5formshim/vendors/colorpicker/js/colorpicker.js"></script>
<!-- Custom input[type=date] widget example -->
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
<link rel="stylesheet" media="screen" type="text/css" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
<link rel="demo" href="http://demo.dsheiko.com/html5formshim" title="HTML5 Form Shim" />
<script src="http://demo.dsheiko.com/share/js/sharebar.js" type="text/javascript"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<header role="banner">
<div>
<hgroup>
<h1>HTML5 Form Shim</h1>
<h2>jQuery plugin to emulate HTML5 Form on legacy browsers + benefits</h2>
</hgroup>
<a class="fork" href="https://github.com/dsheiko/HTML5-Form-Shim/">Fork Me on GitHub</a>
</div>
</header>
<section role="main">
<div>
<article>
<p>
That is a jQuery plugin, which brings <a href="http://www.w3.org/TR/html5/forms.html">HTML5</a> Form support on legacy browsers and allow to extend HTML5 Form on new ones.
Different browsers provide different look&feel for the form elements. However you would hardly able to re-style bubble validation messages , date-picker, color-picker and others. With this plugin you may intercept browser native HTML5 Form API control and, therefore, have all the attached UI components always in the same style. Besides, you can have own validation messaging (e.g. showing messages next to the field instead using tooltips).
</p>
To enable the shim you simply mark forms with <var>data-enable-shim="true"</var> while running jQuery and this plugin.
<textarea rows="7" class="code">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script src="./build/jquery.html5form.min.js" type="text/javascript"></script>
<form data-enable-shim="true">
...
</form>
</textarea>
<p>See details on how top use HTML5 form at <a href="http://www.html5rocks.com/en/tutorials/forms/html5forms/">html5rocks.com</a></p>
<h2>Styling</h2>
<p>Take in the account that legacy browsers ignore CSS pseudo-classes <var>:focus</var>, <var>:invalid</var>, <var>:valid</var>, so use instead classes <var>focus</var>, <var>invalid</var>, <var>valid</var>.</p>
<p>NOTE: The examples below use CSS3 styles. If you want your forms not only behave, but look the same in all browsers, use simple styles</p>
<h2>Features</h2>
<ul>
<li><a href="http://www.w3.org/html/wg/drafts/html/master/forms.html#the-constraint-validation-api">The constraint validation API</a> exposed on HTMLElement for legacy browser and on h5FormShim input wrappers (e.g. <var>input.checkValidity()</var>, <var>input.validationMessage</var>, <var>input.validity.typeMismatch</var> where <var>var input = hfFormShim.getInput("#email")</var>). </li>
<li>Can show <a href="#example2">validation messages on custom elements</a></li>
<li><a href="#example4">Easy attach of 3rd-party-made widgets</a> (datepicker, colorpicker, slider as so on)</li>
<li>Allows <a href="#example1">custom on-submit tooltips</a></li>
<li>Allows <a href="#example3">custom input type validators</a></li>
<li>Allows <a href="#example5">custom on-input callbacks</a></li>
<li>Shim formaction, formenctype, formmethod, and formtarget attributes</li>
<li>Covered with tests</li>
<li>Implemented as UMD (can be used as AMD or CommonJS module)</li>
</ul>
</article>
<h2 id="example1">Example 1: Custom form submission validation</h2>
<div class="table">
<div class="tr">
<div class="td example">
<form class="example1" data-enable-shim="true">
<label for="name">Name:</label>
<input type="text" placeholder="Name" required="required" name="name" />
<label for="email">Email:</label>
<input type="email" placeholder="Email" name="email" />
<label for="name">Age:</label>
<input type="number" required="required" name="age" min="18" max="100" />
<label for="email">Promo code:</label>
<input type="text" name="promocode" required="required" pattern="[A-Z]{3}[0-9]{4}"
title="Promo code consist of 3 uppercase letters followed by 4 digits." />
<label for="email">Tel (e.g. +48 111 11)</label>
<input type="text" name="tel" required="required" pattern="^\+(?:[0-9] ?){6,14}[0-9]$"
title="Please enter valid tel." />
<button class="btn btn-inverse btn-large" type="submit">Submit</button>
</form>
</div>
<div class="td desc">
<p>To make the form showing your custom tooltip on submission validation, define <var>data-custom-validation</var> property on the form element.</p>
<p>The plugin will use <var>$.setCustomValidityCallback</var> to display validation messages. You can override this callback with your own function:</p>
<textarea class="code" rows="12">
$.setCustomValidityCallback = function(error) {
var pos = this.position(),
tooltip = $('<span class="custom-validity-tooltip">' + error
+ '<div><div><!-- --></div></div></span>').appendTo('body');
tooltip.css('top', pos.top - tooltip.height() - 22);
tooltip.css('left', pos.left - 10);
window.setTimeout(function(){
tooltip.remove();
}, 2500);
}
</textarea>
</div>
</div>
</div>
<h2 id="example2">Example 2: Showing validation messages on custom elements</h2>
<div class="table">
<div class="tr">
<div class="td example">
<form class="example2" data-custom-validation="true" data-enable-shim="true">
<label for="email">Email:</label>
<input id="f2email" type="email" placeholder="Email" required="required" name="email" />
<div class="help-inline">
<div data-validation-message-for="f2email"></div>
</div>
<label for="name">Age:</label>
<input id="f2age" type="number" required="required" name="age" min="18" max="100" />
<div class="help-inline">
<div data-validation-message-for="f2age"></div>
</div>
<button class="btn btn-inverse btn-large" type="submit">Submit</button>
</form>
</div>
<div class="td desc">
<p>This form forced to custom validation by attribute <var>data-custom-validation</var>.
So whatever browser you use it displays validation messages on the elements marked as data-validation-message-for="<input-id>"</p>
<textarea rows="3" class="code">
<input id="email" type="email" name="email" />
<div>
<div data-validation-message-for="email"></div>
</div>
</textarea>
</div>
</div>
</div>
<h2 id="example3">Example 3: Custom input type validation</h2>
<div class="table">
<div class="tr">
<div class="td example">
<form class="example3" data-custom-validation="true" data-enable-shim="true">
<label for="name">Zipcode (custom input type):</label>
<input data-type="zip" placeholder="Zipcode" required="required" name="zip" />
<button class="btn btn-inverse btn-large" type="submit">Submit</button>
</form>
</div>
<div class="td desc">
<p>You can easily define your own input type validator by using $.setCustomInputTypeValidator.
It receives following arguments:
</p>
<ul>
<li>(string) Input type (here Zip for <input type="zip">). It takes data-type attribute value if specified (<input data-type="zip">)</li>
<li>(string) Input value validation message</li>
<li>(function) Validation callback (returns boolean)</li>
</ul>
<textarea class="code" rows="4">
$.setCustomInputTypeValidator( "Zip", "Please enter a valid zip code", function() {
var pattern = /^[0-9]{6,8}$/;
return pattern.test( $( this ).val() );
});
</textarea>
<p>
Mark that the input element is available within the callback as the context (this).
</p>
</div>
</div>
</div>
<h2 id="example4">Example 4: Attaching 3rd-party widgets to form elements</h2>
<div class="table">
<div class="tr">
<div class="td example">
<form class="example4" data-enable-shim="true">
<label for="color">Color:</label>
<input type="color" name="color" value="#d4251b" />
<label for="date">Date:</label>
<input type="date" name="date" />
<button class="btn btn-inverse btn-large" type="submit">Submit</button>
</form>
</div>
<div class="td desc">
<p>You can attach to any input element a custom handler.
Thus you can enrich inputs of such types as color, date, datetime, week, moth, time, range with corresponding widgets (e.g. how it's implemented in latest Chrome/Opera releases).
</p>
<p>In the example below you can find color input using <a href="http://www.eyecon.ro/colorpicker/#implement">colorPicker jQuery-plugin</a> and
date input using <a href="http://jqueryui.com/datepicker/">jQueryUI datePicker</a></p>
<textarea rows="10" class="code">
/**
* Custom date picker for color[type="color"]
*
* @param string Input type (here Color for <input type="color">)
* @param string Input value validation message
* @param function Validation callback (returns boolean)
* @param function Initialization callback
*/
$.setCustomInputTypeValidator( "Color", "Please enter a valid hex color", function() {
var pattern = /^#(?:[0-9a-f]{3}){1,2}$/i;
return pattern.test( $( this ).val() );
}, function( control ) {
control.isShimRequired() &&
control.degrade().boundingBox.ColorPicker({
onChange: function( hsb, hex, rgb ) {
control.boundingBox.val( "#" + hex );
},
onBeforeShow: function () {
$( this ).ColorPickerSetColor( this.value );
}
});
});
/**
* Custom date picker for input[type="date"]
*
* @param string Input type (here Date for <input type="date">)
* @param string Input value validation message
* @param function Validation callback (returns boolean)
* @param function Initialization callback
*/
$.setCustomInputTypeValidator( "Date", "Please enter a valid date", function() {
var pattern = /^\d{2,4}(-|\/)\d{2,4}(-|\/)\d{2,4}$/i;
return !$( this ).val() || pattern.test( $( this ).val() );
}, function( control ) {
control.isShimRequired() &&
control.degrade().boundingBox.datepicker();
});
</textarea>
<p>
As you see here last argument of <var>$.setCustomInputTypeValidator</var> is initialization callback. It injects dependency to input control handler (<var>control</var>).
From which you can access the element <var>control.boundingBox</var>(the same as $(this) ) and methods such as <var>control.isShimRequired()</var> (indicates if if the input type supported) and <var>control.degrade()</var> (switch the type to text, which prevents collision with browser-specific element treatment)
</p>
</div>
</div>
</div>
<h2 id="example5">Example 5: Custom oninput callback</h2>
<div class="table">
<div class="tr">
<div class="td example">
<form class="example5" data-custom-validation="true" data-enable-shim="true">
<label for="email">Email:</label>
<input type="email" placeholder="Email" required="required" name="email" />
<label for="password">Password:</label>
<input type="password" required="required" name="password" />
<label for="confirm">Password confirmation:</label>
<input type="password" required="required" name="confirm" />
<button class="btn btn-inverse btn-large" type="submit">Submit</button>
</form>
</div>
<div class="td desc">
<p>HTML5 introduces a new event oninput, which can be handled to perform additional validation
tests on a field. For example, making registration form you can define a handler
which checks by XMLHttpRequest if the given email already exists in DB. Here an example for
a cross-browser on-input handler:
</p>
<textarea class="code" rows="12">
$('form.example3 input[name=confirm]').on("input", function () {
var input = $(this);
if (input.val() != $('form.example3 input[name=password]').val()) {
input.setCustomValidity('The two passwords must match.');
} else {
// input is valid -- reset the error message
input.setCustomValidity('');
}
});
</textarea>
</div>
</div>
</div>
<div class="download">
<a class="btn btn-inverse btn-large" href="hhttps://github.com/dsheiko/HTML5-Form-Shim/downloads">Download the latest version from GitHub</a>
</div>
</div>
</section>
<footer>
<article>
<p>jquery.html5form.js is created by <a href="http://dsheiko.com">Dmitry Sheiko</a>.
It's released under the MIT license. </p>
<p>It's released under the MIT license. If you have any questions or feedback you can use the <a href="https://github.com/dsheiko/HTML5-Form-Shim">github project page.</a>
</p>
</article>
</footer>
<script type="text/javascript">
// Custom input type validator example
$.setCustomInputTypeValidator( "Zip", "Please enter a valid zip code", function() {
var pattern = /^[0-9]{6,8}$/;
return pattern.test( $( this ).val() );
});
// Custom color input handler example
$.setCustomInputTypeValidator( "Color", "Please enter a valid hex color", function() {
var pattern = /^#(?:[0-9a-f]{3}){1,2}$/i;
return pattern.test( $( this ).val() );
}, function( control ) {
control.isShimRequired() &&
control.degrade().boundingBox.ColorPicker({
onChange: function( hsb, hex, rgb ) {
control.boundingBox.val( "#" + hex );
},
onBeforeShow: function () {
$( this ).ColorPickerSetColor( this.value );
}
});
});
// Custom date input handler example
$.setCustomInputTypeValidator( "Date", "Please enter a valid date", function() {
var pattern = /^\d{2,4}(-|\/)\d{2,4}(-|\/)\d{2,4}$/i;
return !$( this ).val() || pattern.test( $( this ).val() );
}, function( control ) {
control.isShimRequired() &&
control.degrade().boundingBox.datepicker();
});
$( document ).ready(function(){
// Using setCustomValidity example
// The setCustomValidity(message), when invoked, must set the custom
// validity error message to the value of the given message argument.
$( "form.example5 input[name=confirm]" ).on( "input", function () {
var input = $( this );
if ( input.val() !== $( "form.example5 input[name=password]" ).val() ) {
input.setCustomValidity( "The two passwords must match." );
} else {
// input is valid -- reset the error message
input.setCustomValidity( "" );
}
});
// Must bound only after DOM is ready, otherwise it will be invoked before H5FormShim's submit handler
$( "form" ).on( "submit", function( e ) {
e.preventDefault();
alert( "Form submitted" );
});
});
</script>
</body>
</html>