-
Notifications
You must be signed in to change notification settings - Fork 8
/
minform.js
82 lines (71 loc) · 2.55 KB
/
minform.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
// minform 2.0 - a minimalist javascript form enhancer
// Copyright 2011, 2013, 2014, 2015 Chris Forno
// Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
// Unsupported browsers:
// Internet Explorer <= 8
(function () {
// We cannot rely on .value since it might be the placeholder text.
function isEmpty(el) {
return el.value === '' || el.value === el.getAttribute('placeholder');
}
function hasClass(el, className) {
if (el.classList) {
return el.classList.contains(className);
} else {
return new RegExp('(^| )' + className + '( |$)', 'gi').test(el.className);
}
}
function removeClass(el, className) {
if (el.classList) {
return el.classList.remove(className);
} else {
return el.className = el.className.replace(new RegExp('(^|\\b)' + className.split(' ').join('|') + '(\\b|$)', 'gi'), ' ');
}
}
function blur(el) {
el.value = el.getAttribute('placeholder');
el.className += ' placeheld';
}
window.minform = function (form) {
// Add placeholder text to elements with an HTML5 placeholder attribute.
// To do so, insert the placeholder value into the input value and add a
// class that allows for CSS styling.
form.addEventListener('blur', function (event) {
if (event.target.hasAttribute('placeholder')) {
if (isEmpty(event.target)) {
blur(event.target);
}
}
}, true);
form.addEventListener('focus', function (event) {
if (event.target.hasAttribute('placeholder') && (hasClass(event.target, 'placeheld'))) {
removeClass(event.target, 'placeheld');
}
}, true);
var placeholders = form.querySelectorAll('[placeholder]');
for (var i = 0; i < placeholders.length; i++) {
blur(placeholders[i]);
}
// Focus the first input with attribute autofocus.
var autofocused = document.querySelector('[autofocus]');
if (autofocused) autofocused.focus();
form.addEventListener('submit', function (event) {
// Don't submit without all required inputs.
var required = form.querySelector('[required]');
for (var i = 0; i < required.length; i++) {
if (isEmpty(required[i])) {
event.preventDefault();
required[i].focus();
return;
}
}
// Clear all placeholder values.
var placeholders = form.querySelectorAll('[placeholder]');
for (var i = 0; i < placeholders.length; i++) {
if (isEmpty(placeholders[i])) {
placeholders[i].value = '';
}
}
});
};
})();