-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.html
executable file
·299 lines (250 loc) · 8.83 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Bouncer.js</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- stylesheets -->
<style type="text/css">
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
font-size: 112.5%;
max-width: 40em;
margin-left: auto;
margin-right: auto;
width: 88%;
}
/**
* Form Styles
*/
label {
display: block;
font-weight: bold;
margin-bottom: 0.5em;
}
.label-normal {
font-weight: normal;
}
.pattern {
color: #808080;
font-size: 0.8em;
font-weight: normal;
}
.supports-color .color,
.supports-date .date,
.supports-time .time,
.supports-month .month {
display: none;
}
input,
select {
display: inline-block;
font-size: 1em;
margin-bottom: 1em;
padding: 0.25em 0.5em;
width: 100%;
}
[type="checkbox"],
[type="radio"] {
margin-bottom: 0.5em;
width: auto;
}
.button {
background-color: #0088cc;
border: 1px solid #0088cc;
border-radius: 1px;
color: #ffffff;
display: inline-block;
font-size: 0.9375em;
font-weight: normal;
line-height: 1.2;
margin-right: 0.3125em;
margin-bottom: 0.3125em;
padding: 0.5em 0.6875em;
width: auto;
}
.button:active,
.button:focus,
.button:hover {
background-color: #005580;
border-color: #005580;
color: #ffffff;
text-decoration: none;
}
.button:active {
box-shadow: inset 0 0.15625em 0.25em rgba(0, 0, 0, 0.15), 0 1px 0.15625em rgba(0, 0, 0, 0.05);
}
/**
* Errors
*/
.error {
border-color: red;
}
.error-message {
color: red;
font-style: italic;
margin-bottom: 1em;
}
</style>
</head>
<body>
<main id="top">
<nav>
<h1>Bouncer.js</h1>
<p><a href="https://github.com/cferdinandi/bouncer">GitHub</a></p>
<hr>
</nav>
<section>
<form class="validate-me" id="validate-me" data-validate>
<div>
<label for="file">Upload any file</label>
<input name="file" id="file" type="file" data-bouncer-target="#file-error-msg" required>
</div>
<div id="file-error-msg"></div>
<div>
<label for="color">Color Picker <span class="pattern color">7-Character Hexadecimal (ex. #f7f7f7)</span></label>
<input type="color" name="color" id="color" pattern="#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})" required>
</div>
<div>
<label for="date">Date <span class="pattern date">YYYY-MM-DD</span></label>
<input type="date" name="date" id="date" required>
</div>
<div>
<label for="time">Time <span class="pattern time">HH:MM (24-hour time)</span></label>
<input type="time" name="time" id="time" required>
</div>
<div>
<label for="month">Month <span class="pattern month">YYYY-MM</span></label>
<input type="month" name="month" id="month" required>
</div>
<div>
<!-- Regex: https://gist.github.com/badsyntax/719800 -->
<label for="email">Email</label>
<input type="email" name="email" id="email" data-bouncer-message="The domain portion of the email address is invalid (the portion after the @)." required>
</div>
<div>
<!-- Regex: https://gist.github.com/dperini/729294 -->
<label for="url">URL</label>
<input type="url" name="url" id="url" data-bouncer-message="The URL is a missing a TLD (for example, .com)." required>
</div>
<div>
<!-- Regex: https://gist.github.com/dperini/729294 -->
<label for="urlnotld">URL without TLD allowed</label>
<input type="url" name="urlnotld" id="urlnotld" pattern="(?:(?:https?|HTTPS?|ftp|FTP):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-zA-Z\u00a1-\uffff0-9]-*)*[a-zA-Z\u00a1-\uffff0-9]+)(?:\.(?:[a-zA-Z\u00a1-\uffff0-9]-*)*[a-zA-Z\u00a1-\uffff0-9]+)*)(?::\d{2,5})?(?:[\/?#]\S*)?" required>
</div>
<div>
<label for="number">Number</label>
<input type="number" name="number" id="number" required>
</div>
<div>
<label for="float">Number (no decimals)</label>
<input type="number" step="any" name="integer" id="integer" pattern="^(?:[-+]?[0-9]*)$" required>
</div>
<div>
<label for="numberminmax">Number with Min and Max <span class="pattern">Must be between 2 and 7</span></label>
<input type="number" min="2" max="7" name="numberminmax" id="numberminmax" required>
</div>
<div>
<label for="tel">Tel <span class="pattern">123-456-7890</span></label>
<input type="text" name="tel" id="tel" pattern="^(?:\d{3}[\-]\d{3}[\-]\d{4})$" required>
</div>
<div>
<label for="password">Password <span class="pattern">At least 1 uppercase character, 1 lowercase character, and 1 number</span></label>
<input type="password" name="password" id="password" data-bouncer-message="Please choose a password that includes at least 1 uppercase character, 1 lowercase character, and 1 number." pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s).*" required>
</div>
<div>
<label for="confirm-password">Confirm Password <span class="pattern">must match the field above</span></label>
<input type="password" name="confirm-password" id="confirm-password" data-bouncer-match="#password" data-bouncer-mismatch-message="Your passwords do not match." required>
</div>
<div>
<label for="textminmax">Text with MinLength and MaxLength <span class="pattern">must be between 3 and 9 characters long</span></label>
<input type="text" minlength="3" maxlength="9" name="textminmax" id="textminmax" required>
</div>
<div>
<label for="select">Select</label>
<select name="select" id="select" required>
<option></option>
<option>Harry Potter</option>
<option>Lord of the Rings</option>
<option>Star Wars</option>
<option>Start Trek</option>
</select>
</div>
<div>
<strong>Radio Buttons</strong>
<label class="label-normal">
<input type="radio" name="radio" id="radio-1" required>
Yes
</label>
<label class="label-normal">
<input type="radio" name="radio" id="radio-2" required>
No
</label>
</div>
<div>
<strong>Checkboxes</strong>
<label class="label-normal">
<input type="checkbox" name="checkbox-1" id="checkbox-1" required>
Wolverine (must be checked)
</label>
<label class="label-normal">
<input type="checkbox" name="checkbox-2" id="checkbox-2">
Storm
</label>
<label class="label-normal">
<input type="checkbox" name="checkbox-3" id="checkbox-3">
Cyclops
</label>
<label class="label-normal">
<input type="checkbox" name="checkbox-4" id="checkbox-4">
Gambit
</label>
</div>
<input type="submit" class="button" value="Submit">
</form>
</section>
</main>
<br><br>
<footer>
<hr>
<p><em>Want to learn how to write your own vanilla JS plugins? Get my free <a href="https://gomakethings.com/newsletter/">daily developer tips</a> and level-up as a web developer. 🚀</em></p>
</footer>
<!-- Javascript -->
<script src="dist/bouncer.polyfills.js"></script>
<script>
var bouncer = new Bouncer('[data-validate]', {
disableSubmit: true,
customValidations: {
valueMismatch: function (field) {
// Look for a selector for a field to compare
// If there isn't one, return false (no error)
var selector = field.getAttribute('data-bouncer-match');
if (!selector) return false;
// Get the field to compare
var otherField = field.form.querySelector(selector);
if (!otherField) return false;
// Compare the two field values
// We use a negative comparison here because if they do match, the field validates
// We want to return true for failures, which can be confusing
return otherField.value !== field.value;
}
},
messages: {
valueMismatch: function (field) {
var customMessage = field.getAttribute('data-bouncer-mismatch-message');
return customMessage ? customMessage : 'Please make sure the fields match.'
}
}
});
document.addEventListener('bouncerFormInvalid', function (event) {
console.log(event.detail.errors);
console.log(event.detail.errors[0].offsetTop);
window.scrollTo(0, event.detail.errors[0].offsetTop);
}, false);
document.addEventListener('bouncerFormValid', function () {
alert('Form submitted successfully!');
window.location.reload();
}, false);
</script>
</body>
</html>