-
Notifications
You must be signed in to change notification settings - Fork 0
/
html5form_beta.js
598 lines (547 loc) · 24 KB
/
html5form_beta.js
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/**
* Html5 Form Plugin - jQuery plugin
* Version 3a
* New structure,
* removed modernizr usage,
* removed languages I don't know and use
*
*
* @Author: René Geuze http://geuze.name
*
* Built for the jQuery library
* http://jquery.com
*
* Supports the following attributes:
* autofocus
* required
* pattern
* placeholder - Not on password and select
* maxlength - textarea only
* novalidate - this is a form attribute
* min - number type only
* max - number type only
*
* Supports the following (new) types
* email
* number
* date - needs datepicker
* datetime - needs datepicker and timepicker
* time - needs timepicker
*
* Uses the following plugins if installed
* jquery.ui.datepicker
* jquery.ui.timepicker
*
*/
(function($) {
"use strict";
$.fn.html5form = function(options) {
var tmp = {},
// Default configuration properties
defaults = {
async: false,
method: false,
responseElement: null,
labels: 'show',
placeholderColor: '#a1a1a1',
action: false,
language: 'en',
emptyMessage: false,
emailMessage: false,
patternMessage: false,
lengthMessage: false,
allBrowsers: false,
debug: false
},
response = {
'en': {
'empty': 'The [title] field is required.',
'email': '[value] is not a valid email address.',
'pattern': 'Pattern missmatch on [title].',
'length': 'Prevented paste because of maximum field length'
},
'nl': {
'empty': '[title] is een verplicht veld.',
'email': '[value] is geen geldig e-mailadres.',
'pattern': '[title] bevat een ongeldige tekenreeks.',
'length': 'Plakken geblokkeerd wegens maximale veldgrootte'
}
},
callbacks = {
ajaxready: function() {},// Runs when retrieving data on async
oninitialize: function() {},// Runs once per form
onsubmit: function() {},// Runs on submit action
onerror: function() {},// Runs on each error
onerrors: function() {},// Runs once for all errors
onfinish: function() {},// Runs once after no errors
onajaxsuccess: function() {},// Runs once if form was sent(only async:true)
onajaxerror: function() {} // Runs once on failure(only async:true)
},
opts = $.extend(
{},
defaults,
callbacks,
options
),
autoFocusHelper = $(':input[autofocus]').first().val(),
dummyInput = document.createElement('input'),
dummyForm = document.createElement('form'),
dummyTextarea = document.createElement('textarea');
// Force debug false on sucky browsers
if (typeof window.console === 'undefined' || typeof window.console.info === 'undefined') {
opts.debug = false;
}
// Our debug output
function debug(str) {
if (opts.debug === true) {
window.console.info(str);
}
}
// Helper function to determine object size
function objSize(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
size++;
}
}
return size;
}
// Figure out what the correct message is for empty field and error field
(function() {
var types, len, i, a, b = 'Message';
// Go to english if the chosen language does not exist
if (typeof response[opts.language] !== 'object') {
opts.language = 'en';
}
types = ['empty', 'email', 'pattern', 'length'];
for (i = 0, len = types.length; i < len; i++) {
a = types[i];
if (opts[a + b] === false) {
opts[a + b] = response[opts.language][a] || response.en[a];
}
}
})();
// Allow some sort of bbcode in messages function
function bbcode(string, element) {
var $element = $(element),
title = $element.attr('title'),
$label = $('label[for="' + ($element.attr('id')) + '"]'),
$parentText = $element.parent().text();
// Replace title
if (string.match('[title]') && title) {
string = string.replace(/\[title\]/gim, title);
} else if (string.match('[title]')) {
string = string.replace(/\[title\]/gim, '[label]');
}
// Replace label
if (string.match('[label]') && $label.length > 0 ) {
string = string.replace(/\[label\]/gim, $('label[for="' + ($element.attr('id')) + '"]').get(0).innerHTML);
} else if(string.match('[label]') && $parentText && $parentText.replace(/\s+/gim, ' ').length > 3) {
string = string.replace(/\[label\]/gim, $parentText.replace(/\s+/gim, ' '));
} else if (string.match('[label]')) {
string = string.replace(/\[label\]/gim, '[name]');
}
// Replace name
if (string.match('[name]') && $element.attr('name')) {
string = string.replace(/\[name\]/gim, $element.attr('name'));
}
// Replace value
if (string.match('[value]')) {
string = string.replace(/\[value\]/gim, $element.val());
}
return string;
}
// Responses
function addResponse(string, element) {
return $(opts.responseElement).append('<p>' + bbcode(string, element) + '</p>').find('p:last');
}
function testForm(form) {
var emptyInput = {},
emailError = {},
patternError = {};
debug('Testing form');
// Clean up current responseElement
$(opts.responseElement).html('');
// Search for empty fields & value same as placeholder
$(':input:visible[required], :input:visible[data-required]', form).each(function() {
var $this = $(this),
name = $this.attr('name'),
value = $this.val();
if (value === $this.attr('placeholder') || value === '') {
emptyInput[name] = value;
debug('Empty input error');
if (opts.onerror(this) === false){
return false;
}
// Show message
if (opts.emptyMessage !== false && opts.responseElement !== null){
addResponse(opts.emptyMessage, $this);
}
}
});
// Check for pattern
$(':input:visible[pattern]', form).each(function() {
var $this = $(this),
name, value, pattern;
name = $this.attr('name');
value = $this.val();
pattern = new RegExp($this.attr('pattern'));
if (!value.match(pattern)) {
patternError[name] = value;
debug('Pattern mismatch');
if (opts.onerror(this) === false) {
return false;
}
// Show message
if (opts.patternMessage !== false && opts.responseDiv !== null) {
addResponse(opts.patternMessage, $this);
}
}
});
// check email type inputs with regular expression
$(':input:visible[type="email"]', form).each(function(index) {
var $this = $(this),
name, value;
name = $this.attr('name');
value = $this.val();
// If empty, skip
if (value === '' || value === this.defaultValue) {
return true;
}
if (value.search(/^.+@.+\.[\w]{2,7}$/i)) {
emailError[name] = value;
debug('Mail error');
if (opts.onerror(this) === false) {
return false;
}
// Show message
if (opts.emailMessage !== false && opts.responseElement !== null) {
addResponse(opts.emailMessage, $this);
}
}
});
// Callback on any errors
if (objSize(emptyInput) > 0 || objSize(emailError) > 0 || objSize(patternError) > 0) {
opts.onerrors(this);
return false;
} else {
return true;
}
}
// Send form
function sendForm(form) {
var $form = $(form),
input = $(':input:visible:not(:button, :submit, :radio, select)', form),
url = opts.action,
method = opts.method,
formData;
if (url === false) {
url = $form.attr('action') || './';
}
if (method === false) {
method = $form.attr('method') || 'get';
}
debug('Sending form');
// Clear all empty value fields before Submit
$(input).each(function() {
var $this = $(this);
if ($this.val() === $this.attr('placeholder')) {
$this.val(this.defaultValue);
}
});
// Submit data by Ajax
if (opts.async === true) {
formData = $form.serialize();
$.ajax({
url : action,
type : method,
data : formData,
success : function(data) {
opts.onajaxsuccess(this);
opts.ajaxready(data);
debug('Success sending');
//Reset form
form.reset();
},
error: function() {
opts.onajaxerror(this);
debug('Error sending');
}
});
} else {
$form.submit();
}
}
// Setup color & placeholder function
function checkPlaceholder(input, action) {
var type = input.attr('type');
// No placeholder and colorchanging on checkbox, select, password and radio
if (type === 'checkbox' || type === 'select' || type === 'radio' || type === 'password') {
return false;
}
// Add placeholder on blur and start
if (action === 'blur' || action === 'start') {
// Do nothing if field has a value
if (input.val() !== '') {
return false;
}
// Change color
input.css('color', opts.placeholderColor);
// Change value
input.val(input.attr('placeholder'));
} else if (action === 'focus'){
if (input.val() === input.attr('placeholder')) {
input.val('');
}
input.css('color', input.attr('data-startcolor'));
}
}
// Check if feature needs to be fixed or not. Using dumb or feature detection
function fixThisOrNot(feature) {
var result = true;
// Always fix if allbrowsers is set to true.
if (opts.allBrowsers === true) {
debug('Forced to be fixed: ' + feature);
result = true;
}
// Test input/textarea/form attributes
else if (feature === 'placeholder' || feature === 'min' || feature === 'max' || feature === 'autofocus' ||
feature === 'required' || feature === 'pattern') {
debug('Input attribute test: ' + feature);
result = feature in dummyInput ? false : true;
}
else if (feature === 'novalidate') {
debug('Form attribute test: ' + feature);
result = feature in dummyForm ? false : true;
}
else if (feature === 'maxLength') {
debug('Textarea attribute test: ' + feature);
result = feature in dummyTextarea ? false : true;
}
// Test input types
else if (feature === 'email' || feature === 'number' || feature === 'date' || feature === 'datetime' ||
feature === 'time') {
debug('Type test: ' + feature);
dummyInput.setAttribute('type', feature);
result = dummyInput.type === feature ? false : true;
}
// For all the stuff I was too lazy
else {
debug('Missing test, please report');
}
return result;
}
// Replaces special type input fields with normal ones for good enough reasons
function replaceType(type, scope) {
var clone;
$('[type="' + type + '"]', scope).each(function() {
var $this = $(this);
try {
// Bypass jQuery to set type on browsers that allow changing it
this.type = 'text';
$this.attr('data-' + type + 'type', type + 'type');
} catch(err) {
debug(err + ' :: Fixing it anyway');
clone = document.createElement('input');
$(clone).data($this.data())
.attr('data-' + type + 'type', type + 'type')
.attr('type', 'text')
.attr('placeholder', $(this).attr('placeholder'))
.attr('name', $(this).attr('name'))
.attr('value', $(this).attr('value'));
console.info(clone);
$this.replaceWith(clone);
}
});
}
// Loop over all forms requested
return this.each(function() {
// Private properties
var $this = $(this),
form = $this,// REMOVE WHEN CHECKED
result = false;
// Callback initialize
opts.oninitialize(this);
debug('oninitialize');
// Label hiding (if required)
if (opts.labels === 'hide') {
$('label', this).hide();
}
// Textarea maxlength
if (fixThisOrNot('maxLength') === true) {
(function(form) {
$('textarea', form).on('keydown', function(event) {
var $this = $(this),
maxlength = ($this.attr('maxlength')) * 1,
keycode = event.which || event.charCode || event.keyCode;
if (keycode === 37 || keycode === 39 || keycode === 8 || keycode === 46) {
return true;
} else if (this.value.length >= maxlength) {
return false;
}
}).on('paste', function(event) {
var $this = $(this),
currentStr = $this.val();
// Need timeout, no other decent way to find pasted text
setTimeout(function() {
var maxlength = ($this.attr('maxlength')) * 1,
newStr = $this.val(),
el;
if(newStr.length > maxlength) {
// TODO Check the spec on what to do. Is cancel ok?
$this.val(currentStr);//newStr.substr(0, maxlength)
el = addResponse(opts.lengthMessage, $this);
setTimeout(function() {
el.fadeOut(500, function() {
$(this).remove();
});
}, 1500);
}
}, 0);
});
})($this);
}
if (fixThisOrNot('number') === true) {
// Number type
$('input[type="number"]', this).on('keydown', function(event) {
var keycode = event.which || event.charCode || event.keyCode,
shift = event.shiftKey || false,
ctrl = event.ctrlKey || false,
alt = event.altKey || false;
// Allow pasting etc while not allowing special ctrl+alt chars
if (ctrl === true && alt === false) {
return true;
// Match numbers
} else if (keycode >= 48 && keycode <= 57 && shift === false) {
return true;
// Match numpad numbers
} else if (keycode >= 96 && keycode <= 105) {
return true;
// Match arrows
} else if ((keycode >= 37 && keycode <= 40) || keycode === 9) {
return true;
// Match .
} else if (keycode === 110 || keycode === 190) {
return true;
// Match -
} else if (keycode === 189 || keycode === 109) {
return true;
// Match del(46) and backspace(8) and enter(13) and tab (9)
} else if (keycode === 46 || keycode === 8 || keycode === 13) {
return true;
// Match home(36) and end(35)
} else if (keycode === 35 || keycode === 36) {
return true;
// Not a valid key pressed
} else {
return false;
}
}).on('keyup', function() {
var $this = $(this),
value = $this.val(),
min = ($this.attr('min')) ? ($this.attr('min')) * 1 : false,
max = ($this.attr('max')) ? ($this.attr('max')) * 1 : false;
// Return if empty
if (value === '') {
return true;
}
// Check for invalid chars(somehow)
if (value.match(/[^\-\.0-9]+/)) {
value = value.replace(/[^\-\.0-9]+/, '');
$this.val(value);
}
// Check for min
if (min !== false && ($this.val()) * 1 < min) {
$this.val(min);
}
// Check for max
if (max !== false && ($this.val()) * 1 > max) {
$this.val(max);
}
}).on('change', function() {
var $this = $(this),
value = $this.val();
debug('change');
// Check for invalid chars(somehow)
if (value.match(/[^\-\.0-9]+/)) {
value.replace(/[^\-\.0-9]+/g, '');
$this.val(value);
}
}).on('paste', function() {
var $this = $(this);
setTimeout(function() {
$this.trigger('change');
}, 100);
});
}
// Progressive enhancement on time picker
if (fixThisOrNot('time') === true && $.ui && $.ui.timepicker) {
replaceType('time', this);
$('input[type="time"], input[data-timetype]', this).timepicker({});
}
// Progressive enhancement on datetime picker
if (fixThisOrNot('datetime') === true && $.ui && $.ui.datepicker && $.ui.timepicker) {
replaceType('datetime', this);
$('input[type="datetime"], input[data-datetimetype]', this).datetimepicker();
}
// Progressive enhancement on date picker
if (fixThisOrNot('date') === true && $.ui && $.ui.datepicker) {
replaceType('date', this);
$('input[type="date"], input[data-datetype]', this).datepicker();
}
// Some things are just different when all browsers have to be checked :-)
if (opts.allBrowsers === true) {
// Prevent default validation methods
if (!$this.attr('novalidate')) {
$this.attr('novalidate', 'novalidate').attr('data-forced-html5form-validate', 'validate');
}
// Always prevent default required
$(':input[required]', this).attr('data-required', 'required').removeAttr('required');
}
// Placeholder - after type replacers
if (fixThisOrNot('placeholder') === true) {
debug('fixing placeholder');
$(':input[placeholder]:not(:button, :submit)', this).each(function() {
var $this = $(this);
$this.attr('data-startcolor', $this.css('color'));
checkPlaceholder($this, 'start');
}).on('focus', function() {
checkPlaceholder($(this), 'focus');
}).on('blur', function() {
checkPlaceholder($(this), 'blur');
});
}
// Find first autofocus to activate - must stay below placeholder fixer
if (fixThisOrNot('autofocus') === true && document.activeElement && document.activeElement.nodeName.toLowerCase() === 'body') {
// Timeout as IE8 fix
setTimeout(function() {
$(':input[autofocus]').first().val('').focus().val(autoFocusHelper);
}, 100);
}
// Normal submit functionality :-)
form.submit(function(event) {
var $this = $(this),
data = $this.data();
if (data.finished === true) {
return true;
}
event.preventDefault();
opts.onsubmit(this);
if ($this.attr('novalidate') && !$this.attr('data-forced-html5form-validate')) {
result = true;
} else {
result = testForm(this);
}
if (result === true) {
data.finished = true;
if (opts.onfinish(this) !== false ) {
sendForm(this);
}
data.finished = false;
}
return false;
});
});
};
})(jQuery);