| 5048 |
dpurdie |
1 |
<!DOCTYPE html>
|
|
|
2 |
<html>
|
|
|
3 |
<head>
|
|
|
4 |
<meta charset="utf-8" />
|
|
|
5 |
<title>Form Test</title>
|
|
|
6 |
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-wip/css/bootstrap.min.css" />
|
|
|
7 |
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/black-tie/jquery-ui.css" />
|
|
|
8 |
<style>
|
|
|
9 |
|
|
|
10 |
/* Form and inputs */
|
|
|
11 |
form {
|
|
|
12 |
width: 500px;
|
|
|
13 |
margin: 0 auto;
|
|
|
14 |
padding: 20px;
|
|
|
15 |
display: block;
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
input.form-control {
|
|
|
19 |
width: 375px;
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
button, input[type="submit"], .button {
|
|
|
23 |
margin-bottom: 8px;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
/* While server is being requested */
|
|
|
27 |
form.validating-server-side {
|
|
|
28 |
background: #F2F2F2;
|
|
|
29 |
}
|
|
|
30 |
|
|
|
31 |
input.validating-server-side {
|
|
|
32 |
opacity: 0.5;
|
|
|
33 |
background: lightgoldenrodyellow;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/* modify inputs for password strength */
|
|
|
37 |
.password-strength input.form-control {
|
|
|
38 |
width: 375px;
|
|
|
39 |
margin-right: 4px;
|
|
|
40 |
display: inline;
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
.password-strength label {
|
|
|
44 |
display: block;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/* Checkboxes */
|
|
|
48 |
.form-group.check-boxes input {
|
|
|
49 |
margin-left: 10px;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
span.help {
|
|
|
53 |
color: #999 !important;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/* Error container for form C */
|
|
|
57 |
|
|
|
58 |
#error-container div {
|
|
|
59 |
color: red;
|
|
|
60 |
line-height: 140%;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
#error-container div:last-child {
|
|
|
64 |
padding-bottom: 10px;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
</style>
|
|
|
68 |
</head>
|
|
|
69 |
<body>
|
|
|
70 |
<div>
|
|
|
71 |
<form action="" id="form-a" role="form">
|
|
|
72 |
<div class="form-group">
|
|
|
73 |
<label class="control-label" for="inline-suggestions">Inline suggestions</label>
|
|
|
74 |
<input name="inline suggestions" type="text" id="inline-suggestions" class="form-control" data-suggestions="Monkey, Horse, Hound, Fox, Tiger, Elephant" />
|
|
|
75 |
</div>
|
|
|
76 |
|
|
|
77 |
<div class="form-group">
|
|
|
78 |
<label class="control-label" for="country-suggestions">Country suggestions</label>
|
|
|
79 |
<input name="country suggestions" data-validation="country" type="text" id="country-suggestions" class="form-control" />
|
|
|
80 |
</div>
|
|
|
81 |
|
|
|
82 |
<div class="form-group">
|
|
|
83 |
<label class="control-label" for="country-suggestions">Swedish county suggestions</label>
|
|
|
84 |
<div>
|
|
|
85 |
<div>
|
|
|
86 |
<!-- Make sure error class "has-error" gets all the way up to .form-group -->
|
|
|
87 |
<input name="Swedish county suggestion" data-validation="swecounty" type="text" id="swedish-county-suggestions" class="form-control" />
|
|
|
88 |
</div>
|
|
|
89 |
</div>
|
|
|
90 |
</div>
|
|
|
91 |
|
|
|
92 |
<div class="form-group">
|
|
|
93 |
<label class="control-label">Year</label>
|
|
|
94 |
<input name="birth" class="form-control"
|
|
|
95 |
data-validation="date"
|
|
|
96 |
data-validation-format="yyyy/mm/dd"
|
|
|
97 |
data-suggestions="2014/01/15,2014/01/16,2014/01/17" />
|
|
|
98 |
</div>
|
|
|
99 |
|
|
|
100 |
<div class="form-group">
|
|
|
101 |
<label class="control-label">Datepicker</label>
|
|
|
102 |
<input name="birth2" class="form-control"
|
|
|
103 |
data-validation="date"
|
|
|
104 |
data-validation-format="mm/dd/yyyy"
|
|
|
105 |
id="datepicker" />
|
|
|
106 |
</div>
|
|
|
107 |
|
|
|
108 |
<div class="form-group">
|
|
|
109 |
<label class="control-label">Number 0-10 (accepting floats with comma)</label>
|
|
|
110 |
<input name="floats" class="form-control"
|
|
|
111 |
data-validation="number"
|
|
|
112 |
data-validation-allowing="range[0;10], float"
|
|
|
113 |
data-validation-decimal-separator=","
|
|
|
114 |
/>
|
|
|
115 |
</div>
|
|
|
116 |
|
|
|
117 |
<div class="form-group password-strength">
|
|
|
118 |
<label class="control-label" for="password">Display password strength (only strong)</label>
|
|
|
119 |
<input name="password" type="password" id="password" class="form-control" data-validation="strength" data-validation-strength="3" />
|
|
|
120 |
</div>
|
|
|
121 |
|
|
|
122 |
<div class="form-group">
|
|
|
123 |
<label class="control-label">Alphanumeric and -_ and spaces</label>
|
|
|
124 |
<input name="alphanumeric with spaces" class="form-control" name="test" data-validation="alphanumeric" data-validation-allowing="-_ " />
|
|
|
125 |
</div>
|
|
|
126 |
|
|
|
127 |
<div class="form-group">
|
|
|
128 |
<label class="control-label">Alphanumeric only</label>
|
|
|
129 |
<input name="aplhanumeric only" class="form-control" name="test2" data-validation="alphanumeric" />
|
|
|
130 |
</div>
|
|
|
131 |
|
|
|
132 |
<div class="checkbox form-group">
|
|
|
133 |
<label>
|
|
|
134 |
<input name="checkbox" type="checkbox" data-validation="required" /> Must be checked
|
|
|
135 |
</label>
|
|
|
136 |
</div>
|
|
|
137 |
|
|
|
138 |
<div class="form-group">
|
|
|
139 |
<label class="control-label">Must choose one</label>
|
|
|
140 |
<br />
|
|
|
141 |
<input name="radio" type="radio" data-validation="required" value="1" /> A
|
|
|
142 |
<input name="radio" type="radio" value="1" /> B
|
|
|
143 |
<input name="radio" type="radio" value="1" /> C
|
|
|
144 |
<input name="radio" type="radio" value="1" /> D
|
|
|
145 |
</div>
|
|
|
146 |
|
|
|
147 |
<div class="form-group">
|
|
|
148 |
<label class="control-label">Even numbers only</label>
|
|
|
149 |
<input name="even numbers" class="form-control" name="test4" data-validation="even_number" />
|
|
|
150 |
</div>
|
|
|
151 |
|
|
|
152 |
<div class="form-group">
|
|
|
153 |
<label class="control-label">Make a choice</label>
|
|
|
154 |
<br />
|
|
|
155 |
<select name="choice" data-validation="required" data-validation-error-msg="Please make a choice">
|
|
|
156 |
<option value="">- - Choose - -</option>
|
|
|
157 |
<option>A</option>
|
|
|
158 |
<option>B</option>
|
|
|
159 |
<option>C</option>
|
|
|
160 |
<option>D</option>
|
|
|
161 |
</select>
|
|
|
162 |
</div>
|
|
|
163 |
|
|
|
164 |
<div class="form-group">
|
|
|
165 |
<label class="control-label">Text</label>
|
|
|
166 |
(<span id="max-len">20</span> chars left)<br />
|
|
|
167 |
<textarea id="text-area" class="form-control" name="some-text"></textarea>
|
|
|
168 |
</div>
|
|
|
169 |
<div class="form-group">
|
|
|
170 |
<label class="control-label">Server validation</label>
|
|
|
171 |
<input class="form-control" name="code" value="secret"
|
|
|
172 |
data-validation-helssp="The word is "secret""
|
|
|
173 |
data-validation="server"
|
|
|
174 |
data-validation-url="http://formvalidator.net/validate-email.php" />
|
|
|
175 |
</div>
|
|
|
176 |
<div class="form-group">
|
|
|
177 |
<label class="control-label">File validation</label>
|
|
|
178 |
<input type="file" name="some-file1" class="form-control"
|
|
|
179 |
data-validation="size mime required"
|
|
|
180 |
data-validation-error-msg-size="The file cant be larger than 400kb"
|
|
|
181 |
data-validation-error-msg="You must upload an image file (max 400 kb)"
|
|
|
182 |
data-validation-allowing="jpg, png, ico"
|
|
|
183 |
data-validation-max-size="400kb" />
|
|
|
184 |
</div>
|
|
|
185 |
<div class="form-group">
|
|
|
186 |
<label class="control-label">File name</label>
|
|
|
187 |
<input type="text" name="some-file2" class="form-control"
|
|
|
188 |
data-validation="extension required"
|
|
|
189 |
data-validation-error-msg="You must write a file name with extension jpg|png|ico"
|
|
|
190 |
data-validation-allowing="jpg, png, ico"
|
|
|
191 |
/>
|
|
|
192 |
</div>
|
|
|
193 |
<div class="form-group">
|
|
|
194 |
<label class="control-label">
|
|
|
195 |
Callback validation, set this value to "1" and
|
|
|
196 |
validation will fail
|
|
|
197 |
</label>
|
|
|
198 |
<input id="callback" class="form-control" />
|
|
|
199 |
</div>
|
|
|
200 |
|
|
|
201 |
<div class="form-group check-boxes">
|
|
|
202 |
<label>Checkbox group</label><br />
|
|
|
203 |
<label>
|
|
|
204 |
<input type="checkbox" name="box" value="1"
|
|
|
205 |
data-validation="checkbox_group"
|
|
|
206 |
data-validation-qty="1-2" /> 1
|
|
|
207 |
</label>
|
|
|
208 |
<label>
|
|
|
209 |
<input type="checkbox" name="box" value="2" /> 2
|
|
|
210 |
</label>
|
|
|
211 |
<label>
|
|
|
212 |
<input type="checkbox" name="box" value="3" /> 3
|
|
|
213 |
</label>
|
|
|
214 |
<label>
|
|
|
215 |
<input type="checkbox" name="box" value="4" /> 4
|
|
|
216 |
</label>
|
|
|
217 |
<label>
|
|
|
218 |
<input type="checkbox" name="box" value="5" /> 5
|
|
|
219 |
</label>
|
|
|
220 |
</div>
|
|
|
221 |
|
|
|
222 |
<p style="line-height: 200%">
|
|
|
223 |
<input type="submit" class="button">
|
|
|
224 |
<br />
|
|
|
225 |
<button class="button" type="button"
|
|
|
226 |
onclick="alert('Form a is ' + ( $('#form-a').isValid({}, {}, false) ? 'VALID':'NOT VALID'));">
|
|
|
227 |
Test validation via js (<strong>without error messages</strong>)
|
|
|
228 |
</button>
|
|
|
229 |
<br />
|
|
|
230 |
<button class="button" type="button"
|
|
|
231 |
onclick="alert('Form a is ' + ( $('#form-a').isValid() ? 'VALID':'NOT VALID'));">
|
|
|
232 |
Test validation via js (showing error messages)
|
|
|
233 |
</button>
|
|
|
234 |
<br />
|
|
|
235 |
<input type="reset" class="button">
|
|
|
236 |
</p>
|
|
|
237 |
</form>
|
|
|
238 |
<hr />
|
|
|
239 |
<form id="form-b">
|
|
|
240 |
<div class="form-group">
|
|
|
241 |
<label class="control-label">Test</label>
|
|
|
242 |
<input name="test" data-validation="number" type="text" />
|
|
|
243 |
</div>
|
|
|
244 |
<div class="form-group">
|
|
|
245 |
<label class="control-label">Password</label>
|
|
|
246 |
<input name="pass" data-validation="confirmation" type="password" />
|
|
|
247 |
</div>
|
|
|
248 |
<div class="form-group">
|
|
|
249 |
<label class="control-label">Password again</label>
|
|
|
250 |
<input name="pass_confirmation" type="password" />
|
|
|
251 |
</div>
|
|
|
252 |
<p>
|
|
|
253 |
<input type="submit" class="button">
|
|
|
254 |
<input type="reset" class="button">
|
|
|
255 |
</p>
|
|
|
256 |
</form>
|
|
|
257 |
<hr />
|
|
|
258 |
<form id="form-c">
|
|
|
259 |
<div class="form-group">
|
|
|
260 |
<label class="control-label">Country</label>
|
|
|
261 |
<input name="test" data-validation="country" data-validation-error-msg="No valid country given" />
|
|
|
262 |
</div>
|
|
|
263 |
<div class="form-group">
|
|
|
264 |
<label class="control-label">E-mail</label>
|
|
|
265 |
<input name="testmail" data-validation="email" data-validation-error-msg="E-mail is not valid" />
|
|
|
266 |
</div>
|
|
|
267 |
<div class="form-group">
|
|
|
268 |
<label class="control-label">Confirm e-mail</label>
|
|
|
269 |
<input name="test" data-validation="confirmation" data-validation-confirm="testmail" />
|
|
|
270 |
</div>
|
|
|
271 |
<div class="form-group">
|
|
|
272 |
<label class="control-label">Alphanumeric (will only be validated if the checkbox is checked)</label>
|
|
|
273 |
<input name="test2"
|
|
|
274 |
data-validation="alphanumeric"
|
|
|
275 |
data-validation-error-msg="Invalid..."
|
|
|
276 |
data-validation-if-checked="checker" />
|
|
|
277 |
<br />
|
|
|
278 |
<input type="checkbox" name="checker" />
|
|
|
279 |
</div>
|
|
|
280 |
<div id="error-container">
|
|
|
281 |
|
|
|
282 |
</div>
|
|
|
283 |
<p>
|
|
|
284 |
<input type="submit" class="button">
|
|
|
285 |
<input type="reset" class="button">
|
|
|
286 |
</p>
|
|
|
287 |
</form>
|
|
|
288 |
<hr />
|
|
|
289 |
<form id="form-d">
|
|
|
290 |
<h2>HTML5 attributes</h2>
|
|
|
291 |
<div class="form-group">
|
|
|
292 |
<label class="control-label">type="email"</label>
|
|
|
293 |
<input type="text" required="required" list="mejl" />
|
|
|
294 |
<datalist id="mejl">
|
|
|
295 |
<option value="Test">Test</option>
|
|
|
296 |
<option value="test2">test2</option>
|
|
|
297 |
<option value="test3">test3</option>
|
|
|
298 |
</datalist>
|
|
|
299 |
</div>
|
|
|
300 |
<div class="form-group">
|
|
|
301 |
<label class="control-label">type="url" (optional)</label>
|
|
|
302 |
<input type="url" />
|
|
|
303 |
</div>
|
|
|
304 |
<div class="form-group">
|
|
|
305 |
<label class="control-label">type="number"</label>
|
|
|
306 |
<input type="number" required="required" />
|
|
|
307 |
</div>
|
|
|
308 |
<div class="form-group">
|
|
|
309 |
<label class="control-label">type="number"</label>
|
|
|
310 |
<input type="number" required="required" maxlength="30" />
|
|
|
311 |
</div>
|
|
|
312 |
<div class="form-group">
|
|
|
313 |
<label class="control-label">type="number" range[-5;5]</label>
|
|
|
314 |
<input type="number" min="-5" max="5" required="required" />
|
|
|
315 |
</div>
|
|
|
316 |
<div class="form-group">
|
|
|
317 |
<label class="control-label">pattern="^([a-z]+)$"</label>
|
|
|
318 |
<input type="text" name="some-colorz" list="some-colorz" pattern="^([a-z]+)$" required="required" />
|
|
|
319 |
<datalist id="some-colorz" style="display: none">
|
|
|
320 |
<option value="Green">Green</option>
|
|
|
321 |
<option value="Blue">Blue</option>
|
|
|
322 |
<option value="Red">Red</option>
|
|
|
323 |
<option value="Black">Black</option>
|
|
|
324 |
<option value="White">White</option>
|
|
|
325 |
</datalist>
|
|
|
326 |
</div>
|
|
|
327 |
<p>
|
|
|
328 |
<input type="submit" class="button">
|
|
|
329 |
<input type="reset" class="button">
|
|
|
330 |
</p>
|
|
|
331 |
</form>
|
|
|
332 |
</div>
|
|
|
333 |
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
|
|
|
334 |
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
|
|
|
335 |
<script src="jquery.form-validator.js"></script>
|
|
|
336 |
<script>
|
|
|
337 |
(function($, window) {
|
|
|
338 |
|
|
|
339 |
var dev = '.dev'; //window.location.hash.indexOf('dev') > -1 ? '.dev' : '';
|
|
|
340 |
|
|
|
341 |
// setup datepicker
|
|
|
342 |
$("#datepicker").datepicker();
|
|
|
343 |
|
|
|
344 |
// Add a new validator
|
|
|
345 |
$.formUtils.addValidator({
|
|
|
346 |
name : 'even_number',
|
|
|
347 |
validatorFunction : function(value, $el, config, language, $form) {
|
|
|
348 |
return parseInt(value, 10) % 2 === 0;
|
|
|
349 |
},
|
|
|
350 |
borderColorOnError : '',
|
|
|
351 |
errorMessage : 'You have to give an even number',
|
|
|
352 |
errorMessageKey: 'badEvenNumber'
|
|
|
353 |
});
|
|
|
354 |
|
|
|
355 |
window.applyValidation = function(validateOnBlur, forms, messagePosition) {
|
|
|
356 |
if( !forms )
|
|
|
357 |
forms = 'form';
|
|
|
358 |
if( !messagePosition )
|
|
|
359 |
messagePosition = 'top';
|
|
|
360 |
|
|
|
361 |
$.validate({
|
|
|
362 |
form : forms,
|
|
|
363 |
language : {
|
|
|
364 |
requiredFields: 'Du måste bocka för denna'
|
|
|
365 |
},
|
|
|
366 |
validateOnBlur : validateOnBlur,
|
|
|
367 |
errorMessagePosition : messagePosition,
|
|
|
368 |
scrollToTopOnError : true,
|
|
|
369 |
borderColorOnError : 'purple',
|
|
|
370 |
modules : 'security'+dev+', location'+dev+', sweden'+dev+', html5'+dev+', file'+dev+', uk'+dev,
|
|
|
371 |
onModulesLoaded: function() {
|
|
|
372 |
$('#country-suggestions').suggestCountry();
|
|
|
373 |
$('#swedish-county-suggestions').suggestSwedishCounty();
|
|
|
374 |
$('#password').displayPasswordStrength();
|
|
|
375 |
},
|
|
|
376 |
onValidate : function($f) {
|
|
|
377 |
|
| 7246 |
dpurdie |
378 |
//console.log('about to validate form '+$f.attr('id'));
|
| 5048 |
dpurdie |
379 |
|
|
|
380 |
var $callbackInput = $('#callback');
|
|
|
381 |
if( $callbackInput.val() == 1 ) {
|
|
|
382 |
return {
|
|
|
383 |
element : $callbackInput,
|
|
|
384 |
message : 'This validation was made in a callback'
|
|
|
385 |
};
|
|
|
386 |
}
|
|
|
387 |
},
|
|
|
388 |
onError : function($form) {
|
|
|
389 |
alert('Invalid '+$form.attr('id'));
|
|
|
390 |
},
|
|
|
391 |
onSuccess : function($form) {
|
|
|
392 |
alert('Valid '+$form.attr('id'));
|
|
|
393 |
return false;
|
|
|
394 |
}
|
|
|
395 |
});
|
|
|
396 |
};
|
|
|
397 |
|
|
|
398 |
$('#text-area').restrictLength($('#max-len'));
|
|
|
399 |
|
|
|
400 |
window.applyValidation(true, '#form-a', 'top');
|
|
|
401 |
window.applyValidation(false, '#form-b', 'element');
|
|
|
402 |
window.applyValidation(true, '#form-c', $('#error-container'));
|
|
|
403 |
window.applyValidation(true, '#form-d', 'element');
|
|
|
404 |
|
|
|
405 |
// Load one module outside $.validate() even though you do not have to
|
|
|
406 |
$.formUtils.loadModules('date'+dev+'.js', false, false);
|
|
|
407 |
|
|
|
408 |
$('input')
|
|
|
409 |
.on('beforeValidation', function() {
|
| 7246 |
dpurdie |
410 |
//console.log('About to validate input "'+this.name+'"');
|
| 5048 |
dpurdie |
411 |
})
|
|
|
412 |
.on('validation', function(evt, isValid) {
|
|
|
413 |
var validationResult = '';
|
|
|
414 |
if( isValid === null ) {
|
|
|
415 |
validationResult = 'not validated';
|
|
|
416 |
} else if( isValid ) {
|
|
|
417 |
validationResult = 'VALID';
|
|
|
418 |
} else {
|
|
|
419 |
validationResult = 'INVALID';
|
|
|
420 |
}
|
| 7246 |
dpurdie |
421 |
//console.log('Input '+this.name+' is '+validationResult);
|
| 5048 |
dpurdie |
422 |
});
|
|
|
423 |
|
|
|
424 |
})(jQuery, window);
|
|
|
425 |
</script>
|
|
|
426 |
<body>
|
|
|
427 |
</html>
|