-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathbootstrapValidator.js
More file actions
882 lines (813 loc) · 32.8 KB
/
bootstrapValidator.js
File metadata and controls
882 lines (813 loc) · 32.8 KB
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
/**
* BootstrapValidator v0.2.3 (http://github.com/nghuuphuoc/bootstrapvalidator)
*
* A jQuery plugin to validate form fields. Use with Bootstrap 3
*
* @author Nguyen Huu Phuoc <phuoc@huuphuoc.me>
* @copyright (c) 2013 Nguyen Huu Phuoc
* @license MIT
*/
(function($) {
var BootstrapValidator = function(form, options) {
this.$form = $(form);
this.options = $.extend({}, BootstrapValidator.DEFAULT_OPTIONS, options);
if ('disabled' == this.options.live) {
// Don't disable the submit buttons if the live validating is disabled
this.options.submitButtons = null;
}
this.invalidFields = {};
this.xhrRequests = {};
this.numPendingRequests = null;
this.formSubmited = false;
this.submitHandlerCalled = false;
this._init();
};
// The default options
BootstrapValidator.DEFAULT_OPTIONS = {
// The form CSS class
elementClass: 'bootstrap-validator-form',
// Default invalid message
message: 'This value is not valid',
// The submit buttons selector
// These buttons will be disabled when the form input are invalid
submitButtons: 'button[type="submit"]',
// The custom submit handler
// It will prevent the form from the default submitting
//
// submitHandler: function(validator, form) {
// - validator is the BootstrapValidator instance
// - form is the jQuery object present the current form
// }
submitHandler: null,
// Live validating. Can be one of 3 values:
// - enabled: The plugin validates fields as soon as they are changed
// - disabled: Disable the live validating. The error messages are only shown after the form is submitted
// - submitted: The live validating is enabled after the form is submitted
live: 'enabled',
// Map the field name with validator rules
fields: null
};
BootstrapValidator.prototype = {
constructor: BootstrapValidator,
/**
* Init form
*/
_init: function() {
if (this.options.fields == null) {
return;
}
var that = this;
this.$form
// Disable client side validation in HTML 5
.attr('novalidate', 'novalidate')
.addClass(this.options.elementClass)
.on('submit', function(e) {
that.formSubmited = true;
if (that.options.fields) {
for (var field in that.options.fields) {
if (that.numPendingRequests > 0 || that.numPendingRequests == null) {
// Check if the field is valid
var $field = that.getFieldElement(field);
if ($field && $field.data('bootstrapValidator.isValid') !== true) {
that.validateField(field);
}
}
}
if (!that.isValid()) {
that.$form.find(that.options.submitButtons).attr('disabled', 'disabled');
if ('submitted' == that.options.live) {
that.options.live = 'enabled';
that._setLiveValidating();
}
// Focus to the first invalid field
for (var i in that.invalidFields) {
that.getFieldElement(i).focus();
break;
}
e.preventDefault();
} else {
if (!that.submitHandlerCalled && that.options.submitHandler && 'function' == typeof that.options.submitHandler) {
// Avoid calling submitHandler recursively
// in the case user call form.submit() right inside the submitHandler()
that.submitHandlerCalled = true;
that.options.submitHandler.call(that, that, that.$form);
return false;
}
}
}
});
for (var field in this.options.fields) {
this._initField(field);
}
this._setLiveValidating();
},
/**
* Enable live validating
*/
_setLiveValidating: function() {
if ('enabled' == this.options.live) {
var that = this;
// Since this should be called once, I have to disable the live validating mode
this.options.live = 'disabled';
for (var field in this.options.fields) {
(function(field) {
var fields = that.getFieldElements(field);
if (fields && fields.length > 0) {
var $field = $(fields[0]);
var type = $field.attr('type');
if ('radio' == type) {
var event = 'change';
fields.on(event, function() {
that.formSubmited = false;
that.validateField(field);
});
} else {
var event = ('checkbox' == type || 'SELECT' == $field.get(0).tagName) ? 'change' : 'keyup';
$field.on(event, function() {
that.formSubmited = false;
that.validateField(field);
});
}
}
})(field);
}
}
},
/**
* Init field
*
* @param {String} field The field name
*/
_initField: function(field) {
if (this.options.fields[field] == null || this.options.fields[field].validators == null) {
return;
}
var $field = this.getFieldElement(field);
if (null == $field) {
return;
}
// Create a help block element for showing the error
var $parent = $field.parents('.form-group'),
helpBlock = $parent.find('.help-block');
if (helpBlock.length == 0) {
var $small = $('<small/>').addClass('help-block').css('display', 'none').appendTo($parent);
$field.data('bootstrapValidator.error', $small);
// Calculate the number of columns of the label/field element
// Then set offset to the help block element
var label, cssClasses, offset, size;
if (label = $parent.find('label').get(0)) {
// The default Bootstrap form don't require class for label (http://getbootstrap.com/css/#forms)
if (cssClasses = $(label).attr('class')) {
cssClasses = cssClasses.split(' ');
for (var i = 0; i < cssClasses.length; i++) {
if (/^col-(xs|sm|md|lg)-\d+$/.test(cssClasses[i])) {
offset = cssClasses[i].substr(7);
size = cssClasses[i].substr(4, 2);
break;
}
}
}
} else if (cssClasses = $parent.children().eq(0).attr('class')) {
cssClasses = cssClasses.split(' ');
for (var i = 0; i < cssClasses.length; i++) {
if (/^col-(xs|sm|md|lg)-offset-\d+$/.test(cssClasses[i])) {
offset = cssClasses[i].substr(14);
size = cssClasses[i].substr(4, 2);
break;
}
}
}
if (size && offset) {
$small.addClass(['col-', size, '-offset-', offset].join(''))
.addClass(['col-', size, '-', 12 - offset].join(''));
}
} else {
$field.data('bootstrapValidator.error', helpBlock.eq(0));
}
},
/**
* Get field element
*
* @param {String} field The field name
* @returns {jQuery}
*/
getFieldElement: function(field) {
var fields = this.$form.find('[name="' + field + '"]');
return (fields.length == 0) ? null : $(fields[0]);
},
/**
* Get field elements
* Used for radios
*
* @param {String} field The field name
* @returns {jQuery}
*/
getFieldElements: function(field) {
var fields = this.$form.find('[name="' + field + '"]');
return (fields.length == 0) ? null : fields;
},
/**
* Validate given field
*
* @param {String} field The field name
*/
validateField: function(field) {
var $field = this.getFieldElement(field);
if (null == $field) {
// Return if cannot find the field with given name
return;
}
var that = this,
validators = that.options.fields[field].validators;
for (var validatorName in validators) {
if (!$.fn.bootstrapValidator.validators[validatorName]) {
continue;
}
var isValid = $.fn.bootstrapValidator.validators[validatorName].validate(that, $field, validators[validatorName]);
if (isValid === false) {
that.showError($field, validatorName);
break;
} else if (isValid === true) {
that.removeError($field);
}
}
},
/**
* Show field error
*
* @param {jQuery} $field The field element
* @param {String} validatorName
*/
showError: function($field, validatorName) {
var field = $field.attr('name'),
validator = this.options.fields[field].validators[validatorName],
message = validator.message || this.options.message,
$parent = $field.parents('.form-group');
this.invalidFields[field] = true;
// Add has-error class to parent element
$parent.removeClass('has-success').addClass('has-error');
$field.data('bootstrapValidator.error').html(message).show();
this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
},
/**
* Remove error from given field
*
* @param {jQuery} $field The field element
*/
removeError: function($field) {
delete this.invalidFields[$field.attr('name')];
$field.parents('.form-group').removeClass('has-error').addClass('has-success');
$field.data('bootstrapValidator.error').hide();
this.$form.find(this.options.submitButtons).removeAttr('disabled');
},
/**
* Start remote checking
*
* @param {jQuery} $field The field element
* @param {String} validatorName
* @param {XMLHttpRequest} xhr
*/
startRequest: function($field, validatorName, xhr) {
var field = $field.attr('name');
$field.data('bootstrapValidator.isValid', false);
this.$form.find(this.options.submitButtons).attr('disabled', 'disabled');
if(this.numPendingRequests == null){
this.numPendingRequests = 0;
}
this.numPendingRequests++;
// Abort the previous request
if (!this.xhrRequests[field]) {
this.xhrRequests[field] = {};
}
if (this.xhrRequests[field][validatorName]) {
this.numPendingRequests--;
this.xhrRequests[field][validatorName].abort();
}
this.xhrRequests[field][validatorName] = xhr;
},
/**
* Complete remote checking
*
* @param {jQuery} $field The field element
* @param {String} validatorName
* @param {Boolean} isValid
*/
completeRequest: function($field, validatorName, isValid) {
if (isValid === false) {
this.showError($field, validatorName);
} else if (isValid === true) {
this.removeError($field);
$field.data('bootstrapValidator.isValid', true);
}
var field = $field.attr('name');
delete this.xhrRequests[field][validatorName];
this.numPendingRequests--;
if (this.numPendingRequests <= 0) {
this.numPendingRequests = 0;
if (this.formSubmited) {
if (!this.submitHandlerCalled && this.options.submitHandler && 'function' == typeof this.options.submitHandler) {
this.submitHandlerCalled = true;
this.options.submitHandler.call(this, this, this.$form);
} else {
this.$form.submit();
}
}
}
},
/**
* Check the form validity
*
* @returns {Boolean}
*/
isValid: function() {
if (this.numPendingRequests > 0) {
return false;
}
for (var field in this.invalidFields) {
// Don't validate disabled element
if (this.invalidFields[field] && !this.getFieldElement(field).is(':disabled')) {
return false;
}
}
return true;
}
};
// Plugin definition
$.fn.bootstrapValidator = function(options) {
return this.each(function() {
var $this = $(this), data = $this.data('bootstrapValidator');
if (!data) {
$this.data('bootstrapValidator', (data = new BootstrapValidator(this, options)));
}
});
};
// Available validators
$.fn.bootstrapValidator.validators = {};
$.fn.bootstrapValidator.Constructor = BootstrapValidator;
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.between = {
/**
* Return true if the input value is between (strictly or not) two given numbers
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following keys:
* - min
* - max
* - inclusive [optional]: Can be true or false. Default is true
* - message: The invalid message
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
value = parseFloat(value);
return (options.inclusive === true)
? (value > options.min && value < options.max)
: (value >= options.min && value <= options.max);
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.callback = {
/**
* Return result from the callback method
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following keys:
* - callback: The callback method that passes 2 parameters:
* callback: function(fieldValue, validator) {
* // fieldValue is the value of field
* // validator is instance of BootstrapValidator
* }
* - message: The invalid message
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (options.callback && 'function' == typeof options.callback) {
return options.callback.call(this, value, this);
}
return true;
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.creditCard = {
/**
* Return true if the input value is valid credit card number
* Based on https://gist.github.com/DiegoSalazar/4075533
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following key:
* - message: The invalid message
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
// Accept only digits, dashes or spaces
if (/[^0-9-\s]+/.test(value)) {
return false;
}
// The Luhn Algorithm
// http://en.wikipedia.org/wiki/Luhn
value = value.replace(/\D/g, '');
var check = 0, digit = 0, even = false, length = value.length;
for (var n = length - 1; n >= 0; n--) {
digit = parseInt(value.charAt(n), 10);
if (even) {
if ((digit *= 2) > 9) {
digit -= 9;
}
}
check += digit;
even = !even;
}
return (check % 10) == 0;
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.different = {
/**
* Return true if the input value is different with given field's value
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Consists of the following key:
* - field: The name of field that will be used to compare with current one
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
var $compareWith = validator.getFieldElement(options.field);
if ($compareWith && value != $compareWith.val()) {
validator.removeError($compareWith);
return true;
} else {
return false;
}
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.digits = {
/**
* Return true if the input value contains digits only
*
* @param {BootstrapValidator} validator Validate plugin instance
* @param {jQuery} $field Field element
* @param {Object} options
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
return /^\d+$/.test(value);
}
}
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.emailAddress = {
/**
* Return true if and only if the input value is a valid email address
*
* @param {BootstrapValidator} validator Validate plugin instance
* @param {jQuery} $field Field element
* @param {Object} options
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
// Email address regular expression
// http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
var emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return emailRegExp.test(value);
}
}
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.greaterThan = {
/**
* Return true if the input value is greater than or equals to given number
*
* @param {BootstrapValidator} validator Validate plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following keys:
* - value: The number used to compare to
* - inclusive [optional]: Can be true or false. Default is true
* - message: The invalid message
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
value = parseFloat(value);
return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
}
}
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.hexColor = {
/**
* Return true if the input value is a valid hex color
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following keys:
* - message: The invalid message
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.identical = {
/**
* Check if input value equals to value of particular one
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Consists of the following key:
* - field: The name of field that will be used to compare with current one
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
var $compareWith = validator.getFieldElement(options.field);
if ($compareWith && value == $compareWith.val()) {
validator.removeError($compareWith);
return true;
} else {
return false;
}
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.lessThan = {
/**
* Return true if the input value is less than or equal to given number
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following keys:
* - value: The number used to compare to
* - inclusive [optional]: Can be true or false. Default is true
* - message: The invalid message
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
value = parseFloat(value);
return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.notEmpty = {
/**
* Check if input value is empty or not
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var type = $field.attr('type');
if('radio' == type) {
var radioSelector = "input[name=" + $field.attr('name') + "]:checked";
return ($(radioSelector).length > 0);
}
return ('checkbox' == type) ? $field.is(':checked') : ($.trim($field.val()) != '');
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.regexp = {
/**
* Check if the element value matches given regular expression
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Consists of the following key:
* - regexp: The regular expression you need to check
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
return options.regexp.test(value);
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.remote = {
/**
* Request a remote server to check the input value
*
* @param {BootstrapValidator} validator Plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Can consist of the following keys:
* - url
* - data [optional]: By default, it will take the value
* {
* <fieldName>: <fieldValue>
* }
* - message: The invalid message
* @returns {Boolean|String}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
var name = $field.attr('name'), data = options.data;
if (data == null) {
data = {};
}
data[name] = value;
var xhr = $.ajax({
type: 'POST',
url: options.url,
dataType: 'json',
data: data
}).success(function(response) {
var isValid = response.valid === true || response.valid === 'true';
validator.completeRequest($field, 'remote', isValid);
});
validator.startRequest($field, 'remote', xhr);
return 'pending';
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.stringLength = {
/**
* Check if the length of element value is less or more than given number
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options Consists of following keys:
* - min
* - max
* At least one of two keys is required
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
var length = $.trim(value).length;
if ((options.min && length < options.min) || (options.max && length > options.max)) {
return false;
}
return true;
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.uri = {
/**
* Return true if the input value is a valid URL
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options
* @returns {Boolean}
*/
validate: function(validator, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
// Credit to https://gist.github.com/dperini/729294
//
// Regular Expression for URL validation
//
// Author: Diego Perini
// Updated: 2010/12/05
//
// the regular expression composed & commented
// could be easily tweaked for RFC compliance,
// it was expressly modified to fit & satisfy
// these test for an URL shortener:
//
// http://mathiasbynens.be/demo/url-regex
//
// Notes on possible differences from a standard/generic validation:
//
// - utf-8 char class take in consideration the full Unicode range
// - TLDs have been made mandatory so single names like "localhost" fails
// - protocols have been restricted to ftp, http and https only as requested
//
// Changes:
//
// - IP address dotted notation validation, range: 1.0.0.0 - 223.255.255.255
// first and last IP address of each class is considered invalid
// (since they are broadcast/network addresses)
//
// - Added exclusion of private, reserved and/or local networks ranges
//
// Compressed one-line versions:
//
// Javascript version
//
// /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!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-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/i
//
// PHP version
//
// _^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!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-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS
var urlExp = new RegExp(
"^" +
// protocol identifier
"(?:(?:https?|ftp)://)" +
// user:pass authentication
"(?:\\S+(?::\\S*)?@)?" +
"(?:" +
// IP address exclusion
// private & local networks
"(?!10(?:\\.\\d{1,3}){3})" +
"(?!127(?:\\.\\d{1,3}){3})" +
"(?!169\\.254(?:\\.\\d{1,3}){2})" +
"(?!192\\.168(?:\\.\\d{1,3}){2})" +
"(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" +
// IP address dotted notation octets
// excludes loopback network 0.0.0.0
// excludes reserved space >= 224.0.0.0
// excludes network & broacast addresses
// (first & last IP address of each class)
"(?:[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]))" +
"|" +
// host name
"(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)" +
// domain name
"(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*" +
// TLD identifier
"(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))" +
")" +
// port number
"(?::\\d{2,5})?" +
// resource path
"(?:/[^\\s]*)?" +
"$", "i"
);
return urlExp.test(value);
}
};
}(window.jQuery));
;(function($) {
$.fn.bootstrapValidator.validators.usZipCode = {
/**
* Return true if and only if the input value is a valid US zip code
*
* @param {BootstrapValidator} validator The validator plugin instance
* @param {jQuery} $field Field element
* @param {Object} options
* @returns {Boolean}
*/
validate: function(validateInstance, $field, options) {
var value = $field.val();
if (value == '') {
return true;
}
return /^\d{5}([\-]\d{4})?$/.test(value);
}
};
}(window.jQuery));