-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.js
1649 lines (1328 loc) · 62 KB
/
forms.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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*! Form validation - v1.1.1 - 2014-04-09
* https://github.com/bboyle/form-validation
* Copyright (c) 2014 Ben Boyle; Licensed MIT */
(function( $ ) {
'use strict';
var SUBMIT_TOLERANCE = 10000, // milliseconds
DEFAULT_STATUS_HTML = '<div class="status warn"><div class="inner"><h2>Please check your answers</h2><ol></ol></div></div>',
// fields that validate
candidateForValidation = 'input, select, textarea',
// invalidFilter
invalidFilter = function() {
return ! ( this.disabled || this.validity.valid );
},
// follow plugin conventions for storing plugin data
// http://docs.jquery.com/Plugins/Authoring#Data
pluginDataKey = 'formValidation',
pluginData = function( key, value ) {
var dataHash = this.data( pluginDataKey ) || this.data( pluginDataKey, {}).data( pluginDataKey );
if ( typeof key !== 'undefined' ) {
if ( typeof value !== 'undefined' ) {
dataHash[ key ] = value;
return value;
} else if ( typeof dataHash[ key ] !== 'undefined' ) {
return dataHash[ key ];
}
return null;
}
return dataHash;
},
// helper for .label, .hint and .alert
getLabelComponent = function( component, options ) {
return this.map(function( index, domElement ) {
var $element = $( domElement ),
labelElement = null,
foundElement = null;
if ( typeof options === 'object' && options.level === 'group' ) {
foundElement = $element.formValidation( 'group' ).find( component )[ 0 ];
} else if ( $element.is( ':radio, :checkbox' )) {
foundElement = $element.closest( 'fieldset' ).find( component )[ 0 ];
} else {
labelElement = $element.closest( 'form' ).find( 'label[for="' + domElement.id + '"]' );
foundElement = labelElement.children( component )[ 0 ];
if ( ! foundElement ) {
if ( component === '.hint' ) {
labelElement.append( '<small class="hint"></small>' );
foundElement = labelElement.children( component )[ 0 ];
}
}
}
return foundElement;
});
},
changeValidityCheck = function() {
var $this = $( this ),
alertElement = $this.formValidation( 'alert' ),
alertLevel,
invalidContainers
;
// is this control valid?
if ( this.validity.valid ) {
// is it part of a group that contain other invalid controls?
if ( $this.formValidation( 'question' ).find( '.alert' ).filter( alertElement ).length > 0 ) {
alertElement.remove();
} else {
// update message from first invalid field in group
invalidContainers = $this.formValidation( 'group' ).find( candidateForValidation ).filter( invalidFilter );
if ( invalidContainers.length > 0 ) {
alertElement.text( invalidContainers.formValidation( 'getValidationMessage' ));
} else {
// all fields valid
alertElement.remove();
}
}
// remove invalid class from ancestors that do not contain invalid fields
$this.parentsUntil( 'form', '.invalid' ).filter(function() {
return $( this ).find( candidateForValidation ).filter( invalidFilter ).length === 0;
})
// remove .invalid class
.removeClass( 'invalid' )
// remove old alerts (change handler should have already done this)
.find( '.alert' ).remove()
;
} else {
// does alert exist?
if ( alertElement.length === 0 ) {
alertElement = $( '<em class="alert"/>' );
}
// show message
alertElement.text( $this.formValidation( 'getValidationMessage' ));
// append to form
if ( $this.formValidation( 'group' ).hasClass( 'atomic' )) {
alertLevel = { 'level' : 'group' };
}
$this.formValidation( 'label', alertLevel ).parent().find( '.label, abbr[title="(required)"]' ).eq( -1 ).after( alertElement );
// NOTE we don't flag the question as .invalid now
// .invalid only happens on submit, to soften inline validation errors
}
},
// checks for invalid elements
// returns number of invalid elements
submitValidityCheck = function() {
// form object
var form = $( this ).closest( 'form' ),
// invalid fields
invalid = form.find( candidateForValidation ).filter(function invalidFields() {
// skip disabled
if ( this.disabled ) {
return false;
}
// only check radio button groups once (skip individual radio button)
if ( this.type === 'radio' ) {
if ( ! invalidFields.cache ) {
invalidFields.cache = {};
} else if ( invalidFields.cache[ this.name ] === true ) {
return false;
}
invalidFields.cache[ this.name ] = true;
}
return this.validity && ! this.validity.valid;
}),
// alert container
alert = pluginData.call( form, 'summaryElement' ) || pluginData.call( form, 'summaryElement', $( DEFAULT_STATUS_HTML )),
// messages within alert
messages = alert.find( 'ol' ),
// track groups
lastGroupSeen = true
;
if ( invalid.length > 0 ) {
// remove old messages
messages.find( 'li' ).remove();
// add new messages
invalid.each(function() {
// get field
var $this = $( this ),
// get group (if exists)
group = $this.formValidation( 'group' ),
// get label or group label
label = $this.formValidation( 'label', {
level : group.length > 0 ? 'group' : null
}),
labelId,
item
;
// get the label id
if ( label.length > 0 ) {
labelId = label[ 0 ].id || label.generateId( 'label-' + this.id )[ 0 ].id;
} else {
labelId = this.name;
}
// get alert item
item = pluginData.call( $this, 'summaryElement' ) || pluginData.call( $this, 'summaryElement', $( '<li><a href="#' + labelId + '"></a></li>' ));
if ( group.length === 0 || group[ 0 ] !== lastGroupSeen ) {
// update last group seen
lastGroupSeen = group[ 0 ];
// create error message with link to label
item
.find( 'a' )
.text( label.text().replace( /\?$/, '' ) + ': ' + $this.formValidation( 'getValidationMessage' ))
.end()
.appendTo( messages )
;
} else {
// remove from DOM
item.remove();
}
});
}
return invalid.length;
},
submitValidationHandler = function( event ) {
// validate form
var count = submitValidityCheck.call( this ),
form = $( this );
// remove invalid class from questions that do not contain invalid fields
form.find( '.invalid' ).filter(function() {
return $( this ).find( candidateForValidation ).filter( invalidFilter ).length === 0;
})
// remove .invalid class
.removeClass( 'invalid' )
// remove old alerts (change handler should have already done this)
.find( '.alert' ).remove()
;
// anything invalid?
if ( count > 0 ) {
// cancel submit
event.stopImmediatePropagation();
event.preventDefault();
// show the error summary
(function( form ) {
var summary = pluginData.call( form, 'summaryElement' );
// hide any previous status blocks
form.prev( '.status' ).not( summary ).remove();
// show the new summary
form.before( summary.fadeIn() );
// focus/scroll summary element
$( window ).scrollTop( summary.offset().top );
}( form ));
// find all the invalid fields
form.find( candidateForValidation ).filter( invalidFilter ).each(function() {
// update inline alerts
changeValidityCheck.call( this );
})
// set .invalid on ancestor LI elements
.parentsUntil( 'form', '.questions > li' )
// but not sections
.not( '.section, .compact' )
.addClass( 'invalid' )
;
// trigger x-invalid
form.trigger( 'x-invalid' );
// cancel submit
return false;
}
},
// bind this AFTER the validation handler
// only invoked if validation did not prevent submit
submitDoneHandler = function( event ) {
// use event.timeStamp when available and $.now() otherwise
var timeStamp = event.timeStamp || $.now(),
form = $( this ),
summaryElement = pluginData.call( form, 'summaryElement' ),
lastSubmitTimeStamp
;
// remove summary element from DOM on successful submit
if ( summaryElement ) {
summaryElement.remove();
}
// is this submit event too soon after the last one?
lastSubmitTimeStamp = pluginData.call( form, 'lastSubmitTimeStamp' );
if ( lastSubmitTimeStamp && timeStamp - lastSubmitTimeStamp < SUBMIT_TOLERANCE ) {
// cancel the submit event
event.stopImmediatePropagation();
event.preventDefault();
return false;
} else {
// store the timestamp
pluginData.call( form, 'lastSubmitTimeStamp', timeStamp );
}
},
// plugin methods
methods = {
// $( x ).formValidation( 'alert' ) -- get
// get alert text
alert : function() {
return this.map(function( index, domElement ) {
var $element = $( domElement ),
group;
if ( $element.is( ':radio, :checkbox' ) === true ) {
return $element.closest( 'fieldset' ).find( 'legend > .alert' )[ 0 ];
} else {
// atomic groups
group = $element.formValidation( 'group' ).filter( '.atomic' );
if ( group.length > 0 ) {
return group.find( 'legend > .alert' )[ 0 ];
} else {
return $( 'label[for="' + domElement.id + '"] > .alert' )[ 0 ];
}
}
});
},
// $( x ).formValidation( 'label' )
// $( x ).formValidation( 'label', { level : group })
// return .label associated with element or containing group
label : function( options ) {
return getLabelComponent.call( this, '.label', options );
},
// $( x ).formValidation( 'hint' )
// $( x ).formValidation( 'hint', { level : group })
// return .hint associated with element or containing group
hint : function( options ) {
return getLabelComponent.call( this, '.hint', options );
},
// $( x ).formValidation( 'question' )
// return question element for item
question : function( options ) {
// looking for group?
if ( typeof options === 'object' && options.level === 'group' ) {
// return the group
return this.formValidation( 'group' );
}
// not looking for group
return this.map(function( index, domElement ) {
return $( domElement ).parentsUntil( 'form', '.questions > li' )[ 0 ];
});
},
// $( x ).formValidation( 'group' )
// return group element for item
group : function() {
return this.map(function( index, domElement ) {
return $( domElement ).parentsUntil( 'form', '.group' ).filter(function() {
// ignore groups that do not contain fieldsets
return $( this ).children( 'fieldset' ).length > 0;
})[ 0 ];
});
},
// $( x ).formValidation( 'validate' )
// binds validation handler functions
// sets @novalidate on form to disable built-in validation
// TODO allow this to be called multiple times without binding additional handlers!
validate : function() {
return this.each(function() {
$( this ).closest( 'form' )
// turn off native validation
.attr( 'novalidate', true )
// unbind and rebind handlers
.unbind( 'submit', submitDoneHandler )
.unbind( 'submit', submitValidationHandler )
// validate this form
.bind( 'submit', submitValidationHandler )
// if validation did not cancel submit…
.bind( 'submit', submitDoneHandler )
// bind inline validation handlers to form elements
.find( candidateForValidation )
.unbind( 'change', changeValidityCheck )
.bind( 'change', changeValidityCheck )
;
});
},
// $( x ).formValidation( 'getValidationMessage' )
// return String validation message, e.g. "Must be completed"
getValidationMessage : function() {
var validityState = this[ 0 ].validity;
if ( typeof validityState === 'undefined' || validityState.valid === true ) {
return '';
} else if ( validityState.valueMissing ) {
return 'Must be completed';
} else if ( validityState.customError ) {
return this[ 0 ].validationMessage;
} else if ( validityState.typeMismatch ) {
return 'Must be an email address';
} else if ( validityState.patternMismatch ) {
return 'Must use the format shown';
} else {
return 'Must be a valid answer';
}
}
};
$.fn.formValidation = function( method ) {
// Method calling logic
// http://docs.jquery.com/Plugins/Authoring#Plugin_Methods
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.formValidation' );
}
};
// legacy API
$.fn.forcesForms = $.fn.formValidation;
}( jQuery ));
/*! Generate ID - v1.0.3 - 2014-09-18
* https://github.com/bboyle/Generate-ID
* Copyright (c) 2014 Ben Boyle; Licensed MIT */
(function( $ ) {
'use strict';
/**
* Assigns a unique value to `@id` unless hasAttribute( 'id' ) is true
*
* @param preferredId string to use for id value
*
* @return jquery object (chaining supported)
*/
$.fn.generateId = function( preferredId ) {
var i = 1;
if ( ! preferredId ) {
preferredId = 'id';
} else {
preferredId = $.trim( preferredId.toLowerCase().replace( /[^a-z0-9_]+/g, ' ' )).replace( /\s+/g, '-' );
}
return this.each(function() {
var id;
if ( ! this.getAttribute( 'id' )) {
id = preferredId;
while ( document.getElementById( id )) {
id = preferredId + String( i );
i++;
}
this.setAttribute( 'id', id );
}
});
};
}( jQuery ));
/*! HTML5 constraintValidationAPI - v1.0.7 - 2015-02-19
* https://github.com/bboyle/html5-constraint-validation-API
* Copyright (c) 2015 Ben Boyle; Licensed MIT */
/*exported initConstraintValidationAPI*/
if ( jQuery !== 'undefined' ) {
(function( $ ) {
'use strict';
// http://www.whatwg.org/specs/web-apps/current-work/multipage/states-of-the-type-attribute.html#valid-e-mail-address
// 1*( atext / "." ) "@" ldh-str 1*( "." ldh-str )
var REXP_EMAIL = /^[A-Za-z0-9!#$%&'*+\-\/=\?\^_`\{\|\}~\.]+@[A-Za-z0-9\-]+(\.[A-Za-z0-9\-]+)*$/,
// fields that validate
candidateForValidation = 'input, select, textarea',
// for feature detection
input = $( '<input>' ).get( 0 ),
// polyfill test
polyfill = typeof input.validity !== 'object',
// radio button bug (google earth internal browser)
radioButtonBug = ! polyfill && $( '<input type="radio" required checked>' ).get( 0 ).validity.valueMissing === true,
validateBuggyRadioButtons,
// invalid fields filter
isInvalid = function() {
return ! ( this.disabled || this.validity.valid );
},
// get all radio buttons
getRadioButtonsInGroup = function( radio ) {
return $( radio.form.elements[ radio.name ] ).filter( '[name="' + radio.name + '"]' );
},
// manage validity state object
validityState = function( typeMismatch, valueMissing, customError, message, patternMismatch ) {
if ( typeof message === 'string' ) {
customError = !! message;
}
return {
customError: customError,
typeMismatch: !! typeMismatch,
patternMismatch: !! patternMismatch,
valueMissing: !! valueMissing,
valid: ! valueMissing && ! customError && ! typeMismatch && ! patternMismatch
};
},
validateField = function( message ) {
var $this = $( this ),
required = !! $this.attr( 'required' ),
radio = this.type === 'radio' && getRadioButtonsInGroup( this ),
valueMissing,
invalidEmail = this.getAttribute( 'type' ) === 'email' && !! this.value && ! REXP_EMAIL.test( this.value ),
patternMismatch,
pattern,
newValidityState
;
// radio buttons are required if any single radio button is flagged as required
if ( radio && ! required ) {
required = radio.filter( '[required]' ).length > 0;
}
// if required, check for missing value
if ( required ) {
if ( /^select$/i.test( this.nodeName )) {
valueMissing = this.selectedIndex === 0 && this.options[ 0 ].value === '';
} else if ( radio ) {
valueMissing = radio.filter( ':checked' ).length === 0;
} else if ( this.type === 'checkbox' ) {
valueMissing = ! this.checked;
} else {
valueMissing = ! this.value;
}
}
if ( !! this.getAttribute( 'pattern' ) ) {
if ( this.value.length > 0 ) {
// http://www.whatwg.org/specs/web-apps/current-work/multipage/common-input-element-attributes.html#compiled-pattern-regular-expression
pattern = new RegExp( '^(?:' + this.getAttribute( 'pattern' ) + ')$' );
patternMismatch = ! pattern.test( this.value );
} else {
patternMismatch = false;
}
}
// set .validityState
newValidityState = validityState( invalidEmail, valueMissing, this.validity.customError || false, message, patternMismatch );
if ( radio ) {
getRadioButtonsInGroup( this ).each(function() { this.validity = newValidityState; });
} else {
this.validity = newValidityState;
}
// set .validationMessage
if ( this.validity.valid ) {
this.validationMessage = '';
} else if ( this.validity.customError ) {
if ( typeof message === 'string' ) {
this.validationMessage = message;
}
} else if ( this.validity.valueMissing ) {
this.validationMessage = 'Please answer this question';
} else if ( this.validity.typeMismatch ) {
this.validationMessage = 'Please type an email address';
} else if ( this.validity.patternMismatch ) {
this.validationMessage = 'Please use the format shown';
} else {
this.validationMessage = 'Please answer the question correctly';
}
return this.disabled || this.validity.valid;
},
changeHandler = function( event ) {
var target = event.target;
validateField.call( target );
if ( target.type === 'radio' ) {
getRadioButtonsInGroup( target ).each(function() {
this.validity = target.validity;
this.validationMessage = target.validationMessage;
});
}
},
submitHandler = function( event ) {
var form = $( this ),
novalidate = !! form.attr( 'novalidate' ),
invalid = false
;
// polyfill validation?
if ( polyfill ) {
// check fields
form.find( candidateForValidation ).each(function() {
invalid = ! validateField.call( this );
// unless @novalidate
if ( ! novalidate ) {
// if invalid
if ( invalid ) {
// use triggerHandler because invalid does not bubble
$( this ).triggerHandler( 'invalid' );
}
}
});
}
// NOTE all the code below runs in all browsers to polyfill implementation bugs
// required radio button check
if ( radioButtonBug ) {
validateBuggyRadioButtons( this );
}
// Opera 11 on OSX fires submit event even when fields are invalid
// correct implementations will not invoke this submit handler until all fields are valid
// unless @novalidate
// if there are invalid fields
if ( ! novalidate && form.find( candidateForValidation ).filter( isInvalid ).length > 0 ) {
// abort submit
event.stopImmediatePropagation();
event.preventDefault();
return false;
}
},
initConstraintValidationAPI = function() {
var candidates = $( candidateForValidation );
// INPUT validityState
if ( polyfill ) {
// set us up the API
candidates.filter(function() {
return typeof this.validity !== 'object';
}).each(function() {
this.validity = validityState( false, false, false, '', false );
this.validationMessage = '';
});
// check validity on change
candidates
.unbind( 'change.constraintValidationAPI' )
.bind( 'change.constraintValidationAPI', changeHandler )
;
}
// INPUT validitationMessage
if ( typeof input.validationMessage !== 'string' ) {
// set us up the API
candidates.filter(function() {
return typeof this.validationMessage !== 'string';
}).each(function() {
this.validationMessage = '';
});
}
// INPUT checkValidity
if ( typeof input.checkValidity !== 'function' ) {
// set us up the API
candidates.filter(function() {
return typeof this.checkValidity !== 'function';
}).each(function() {
var domElement = this;
this.checkValidity = function() {
var valid = validateField.call( domElement );
// if invalid, and unless novalidate
if ( ! valid && ! this.form.getAttribute( 'novalidate' )) {
// use triggerHandler because invalid does not bubble
$( domElement ).triggerHandler( 'invalid' );
}
return valid;
};
});
}
// INPUT setCustomValidity
if ( typeof input.setCustomValidity !== 'function' ) {
// set us up the API
candidates.filter(function() {
return typeof this.setCustomValidity !== 'function';
}).each(function() {
var that = this;
this.setCustomValidity = function( message ) {
validateField.call( that, message );
};
});
}
// check for required radio button bug (google earth internal browser)
if ( radioButtonBug ) {
validateBuggyRadioButtons = function( form ) {
var seen = {};
var radio, valueMissing;
// check every required radio button
$( 'input', form ).filter( ':radio' ).filter( '[required],[aria-required="true"]' ).each(function() {
if ( typeof seen[ this.name ] === 'undefined' ) {
seen[ this.name ] = true;
radio = getRadioButtonsInGroup( this );
valueMissing = radio.filter( ':checked' ).length === 0;
if ( valueMissing ) {
// make sure @required is set to use validation API
radio.attr( 'required', 'required' );
} else {
// using @aria-required=true so we can track this control
// removing @required here to bypass validation bug
radio.attr( 'aria-required', true ).removeAttr( 'required' );
}
}
});
};
// initial validity
$( 'form' ).each( validateBuggyRadioButtons );
// watch changes
if ( ! polyfill ) {
candidates.filter( ':radio' )
.unbind( 'change.constraintValidationAPI' )
.bind( 'change.constraintValidationAPI', function() {
validateBuggyRadioButtons( this.form );
})
;
}
}
// check validity on submit
// this should be bound before all other submit handlers bound to the same form
// otherwise they will execute before this handler can cancel submit (oninvalid)
$( 'form' )
.unbind( 'submit.constraintValidationAPI' )
.bind( 'submit.constraintValidationAPI', submitHandler )
;
}
;
// run immediately and ondocumentready
initConstraintValidationAPI();
$( initConstraintValidationAPI );
// expose init function
window.initConstraintValidationAPI = initConstraintValidationAPI;
}( jQuery ));
}
/*
* jQuery Simply Countable plugin
* Provides a character counter for any text input or textarea
*
* @version 0.4.2
* @homepage http://github.com/aaronrussell/jquery-simply-countable/
* @author Aaron Russell (http://www.aaronrussell.co.uk)
*
* Copyright (c) 2009-2010 Aaron Russell (aaron@gc4.co.uk)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*/
(function($){
$.fn.simplyCountable = function(options){
options = $.extend({
counter: '#counter',
countType: 'characters',
maxCount: 140,
strictMax: false,
countDirection: 'down',
safeClass: 'safe',
overClass: 'over',
thousandSeparator: ',',
onOverCount: function(){},
onSafeCount: function(){},
onMaxCount: function(){}
}, options);
var navKeys = [33,34,35,36,37,38,39,40];
return $(this).each(function(){
var countable = $(this);
var counter = $(options.counter);
if (!counter.length) { return false; }
var countCheck = function(){
var count;
var revCount;
var reverseCount = function(ct){
return ct - (ct*2) + options.maxCount;
}
var countInt = function(){
return (options.countDirection === 'up') ? revCount : count;
}
var numberFormat = function(ct){
var prefix = '';
if (options.thousandSeparator){
ct = ct.toString();
// Handle large negative numbers
if (ct.match(/^-/)) {
ct = ct.substr(1);
prefix = '-';
}
for (var i = ct.length-3; i > 0; i -= 3){
ct = ct.substr(0,i) + options.thousandSeparator + ct.substr(i);
}
}
return prefix + ct;
}
var changeCountableValue = function(val){
countable.val(val).trigger('change');
}
/* Calculates count for either words or characters */
if (options.countType === 'words'){
count = options.maxCount - $.trim(countable.val()).split(/\s+/).length;
if (countable.val() === ''){ count += 1; }
}
else { count = options.maxCount - countable.val().length; }
revCount = reverseCount(count);
/* If strictMax set restrict further characters */
if (options.strictMax && count <= 0){
var content = countable.val();
if (count < 0) {
options.onMaxCount(countInt(), countable, counter);
}
if (options.countType === 'words'){
var allowedText = content.match( new RegExp('\\s?(\\S+\\s+){'+ options.maxCount +'}') );
if (allowedText) {
changeCountableValue(allowedText[0]);
}
}
else { changeCountableValue(content.substring(0, options.maxCount)); }
count = 0, revCount = options.maxCount;
}
counter.text(numberFormat(countInt()));
/* Set CSS class rules and API callbacks */
if (!counter.hasClass(options.safeClass) && !counter.hasClass(options.overClass)){
if (count < 0){ counter.addClass(options.overClass); }
else { counter.addClass(options.safeClass); }
}
else if (count < 0 && counter.hasClass(options.safeClass)){
counter.removeClass(options.safeClass).addClass(options.overClass);
options.onOverCount(countInt(), countable, counter);
}
else if (count >= 0 && counter.hasClass(options.overClass)){
counter.removeClass(options.overClass).addClass(options.safeClass);
options.onSafeCount(countInt(), countable, counter);
}
};
countCheck();
countable.on('keyup blur paste', function(e) {
switch(e.type) {
case 'keyup':
// Skip navigational key presses
if ($.inArray(e.which, navKeys) < 0) { countCheck(); }
break;
case 'paste':
// Wait a few miliseconds if a paste event
setTimeout(countCheck, (e.type === 'paste' ? 5 : 0));
break;
default:
countCheck();
break;
}
});
});
};
})(jQuery);/*! relevance - v2.1.0 - 2015-03-04
* https://github.com/bboyle/relevance
* Copyright (c) 2015 Ben Boyle; Licensed MIT */
if ( jQuery !== 'undefined' ) {
(function( $ ) {
'use strict';
var relevantEvent = 'relevant',
irrelevantEvent = 'irrelevant',
elementsToDisable = 'button, input, select, textarea',
polyfillHidden = (function() {
var hidden = $( '<div hidden></div>' );
var hiddenSupported = hidden.appendTo( 'body' ).is( ':hidden' );
hidden.remove();
return ! hiddenSupported;
}()),
formElementsByName = function( form, name ) {
// filter out the @id matching of HTMLFormElement.elements[]
return $( form.elements[ name ] ).filter( '[name="' + name +'"]' );
},
filterRelevant = function() {
return $( this ).closest( '[hidden]' ).length === 0;
},
filterIrrelevant = function() {
return $( this ).closest( '[hidden]' ).length > 0;
},
valueMap = function( element ) {
return element.value;
},
valueInArray = function( possibleValues, actualValues ) {
var i;
if ( typeof possibleValues !== 'object' ) {
possibleValues = [ possibleValues ];
}
for ( i = 0; i < actualValues.length; i++ ) {
if ( $.inArray( actualValues[ i ], possibleValues ) !== -1 ) {
return true;
}