-
Notifications
You must be signed in to change notification settings - Fork 0
/
html5form.js
582 lines (535 loc) · 23.8 KB
/
html5form.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
/**
* Html5 Form Plugin - jQuery plugin
* Version 2.2
*
* @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
* modernizr.input - Might be removed in v3
* modernizr.inputtypes - Might be removed in v3
* jquery.ui.datepicker
* jquery.ui.timepicker
*
*/
(function($) {
// Uncomment string below during testing
"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': 'Please type 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'
},
'es': {
'empty': 'El campo [title] es requerido.',
'email': 'Ingrese una dirección de correo válida por favor.'
},
'it': {
'empty': 'Il campo [title] é richiesto.',
'email': 'L\'indirizzo e-mail non é valido.'
},
'de': {
'empty': '[title] ist ein Pflichtfeld.',
'email': 'Bitte eine gültige E-Mail-Adresse eintragen.'
},
'fr': {
'empty': 'Le champ [title] est requis.',
'email': 'Entrez une adresse email valide s’il vous plait.'
},
'br': {
'empty': 'O campo [title] é obrigatório.',
'email': 'Insira um email válido por favor.'
}
},
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
),
Modernizr = window.Modernizr || false,
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) {
if (string.match('[title]') && $(element).attr('title')) {
string = string.replace('[title]', $(element).attr('title'));
} else if (string.match('[title]')) {
string = string.replace('[title]', '[label]');
}
if (string.match('[label]') && $('label[for="' + ($(element).attr('id')) + '"]').length > 0 ) {
string = string.replace('[label]', $('label[for="' + ($(element).attr('id')) + '"]').get(0).innerHTML);
} else if($(element).parent().text().replace(/\s+/gim, ' ').length > 3) {
string = string.replace('[label]', $(element).parent().text().replace(/\s+/gim, ' '));
} else if (string.match('[label]')) {
string = string.replace('[label]', '[name]');
}
if (string.match('[name]') && $(element).attr('name')) {
string = string.replace('[name]', $(element).attr('name'));
}
if (string.match('[value]')) {
string = string.replace('[value]', $(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 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 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, element) {
var name, value;
name = $(this).attr('name');
value = $(this).val();
// If empty, skip
if (value === '' || value === element.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 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() {
if ($(this).val() === $(this).attr('placeholder')) {
$(this).val($(this).get(0).defaultValue);
}
});
// Submit data by Ajax
if (opts.async === true) {
formData = $(form).serialize();
$.ajax({
url : opts.action,
type : opts.method,
data : formData,
success : function(data){
opts.onajaxsuccess(this);
opts.ajaxready(data);
debug('Success sending');
//Reset form
$(':input', form).each(function() {
$(this).val($(this).get(0).defaultValue);
});
},
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;
}
// If we have modernizr, use this smart stuff :-)
else if (Modernizr !== false && Modernizr.inputtypes && Modernizr.inputtypes[feature]) {
debug('Modernizr test');
result = !Modernizr.inputtypes[feature];
}
else if (Modernizr !== false && Modernizr.input && Modernizr.input[feature]) {
debug('Modernizr test');
result = !Modernizr.input[feature];
}
// 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() {
$clone = $(this).clone( true, true );
$clone.attr('data-' + type + 'type', type + 'type').attr('type', 'text');
$(this).replaceWith($clone);
});
}
// Loop over all forms requested
return this.each(function() {
// Private properties
var form = $(this),
result = false;
// Callback initialize
opts.oninitialize(this);
debug('oninitialize');
// Label hiding (if required)
if (opts.labels === 'hide') {
$(this).find('label').hide();
}
// Placeholder
if (fixThisOrNot('placeholder') === true) {
debug('fixing placeholder');
$(':input[placeholder]:not(:button, :submit)', form).each(function() {
$(this).attr('data-startcolor', $(this).css('color'));
checkPlaceholder($(this), 'start');
}).on('focus', function() {
checkPlaceholder($(this), 'focus');
}).on('blur', function() {
checkPlaceholder($(this), 'blur');
});
}
// Textarea maxlength
if (fixThisOrNot('maxLength') === true) {
(function(form) {
$('textarea', form).on('keydown', function(event) {
var 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);
});
})(form);
}
if (fixThisOrNot('number') === true) {
// Number type
$('input[type="number"]', form).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 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 value = $(this).val();
// Check for invalid chars(somehow)
if (value.match(/[^\-\.0-9]+/)) {
value.replace(/[^\-\.0-9]+/g, '');
$(this).val(value);
}
});
}
// Progressive enhancement on time picker
if (fixThisOrNot('time') === true && $.ui && $.ui.datepicker && $.ui.timepicker) {
replaceType('time', form);
$('input[type="time"], input[data-timetype]', form).timepicker({});
}
// Progressive enhancement on datetime picker
if (fixThisOrNot('datetime') === true && $.ui && $.ui.datepicker && $.ui.timepicker) {
replaceType('datetime', form);
$('input[type="datetime"], input[data-datetimetype]', form).datetimepicker();
}
// Progressive enhancement on date picker
if (fixThisOrNot('date') === true && $.ui && $.ui.datepicker) {
replaceType('date', form);
$('input[type="date"], input[data-datetype]', form).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]', form).attr('data-required', 'required').removeAttr('required');
}
// Normal submit functionality :-)
form.submit(function(event) {
if (tmp.finished === true) {
return true;
}
event.preventDefault();
opts.onsubmit(this);
if ($(this).attr('novalidate') && !$(this).attr('data-forced-html5form-validate')) {
result = true;
} else {
result = testForm(form);
}
if (result === true) {
tmp.finished = true;
if (opts.onfinish(form) !== false ) {
sendForm(form);
}
tmp.finished = false;
}
return false;
});
});
// Find first autofocus to activate - must stay below placeholder fixer, which is in the loop
if (fixThisOrNot('autofocus') === true && !$(document.activeElement).is('body')) {
$(':input[autofocus]').first().val('').focus().val(autoFocusHelper);
}
};
})(jQuery);