Skip to content

Commit 600d139

Browse files
committed
Fix #32, #43, #47
1 parent f7eaee0 commit 600d139

19 files changed

Lines changed: 158 additions & 36 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* [#34: Avoid from calling form submit recursively](https://github.com/nghuuphuoc/bootstrapvalidator/issues/34)
88
* [#39: Validate existing fields only](https://github.com/nghuuphuoc/bootstrapvalidator/issues/39)
99
* [#40: Fix the issue when the form label doesn't have class](https://github.com/nghuuphuoc/bootstrapvalidator/issues/40)
10+
* [#43: Only validate not empty field](https://github.com/nghuuphuoc/bootstrapvalidator/issues/43)
1011

1112
##v0.2.1 (2013-11-08)
1213

demo/index.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,6 @@ <h2>Sign up</h2>
102102
},
103103
email: {
104104
validators: {
105-
notEmpty: {
106-
message: 'The email address is required and can\'t be empty'
107-
},
108105
emailAddress: {
109106
message: 'The input is not a valid email address'
110107
}

dist/js/bootstrapValidator.js

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,12 @@
383383
* @returns {Boolean}
384384
*/
385385
validate: function(validator, $field, options) {
386-
var value = parseFloat($field.val());
386+
var value = $field.val();
387+
if (value == '') {
388+
return true;
389+
}
390+
391+
value = parseFloat(value);
387392
return (options.inclusive === true)
388393
? (value > options.min && value < options.max)
389394
: (value >= options.min && value <= options.max);
@@ -408,6 +413,10 @@
408413
*/
409414
validate: function(validator, $field, options) {
410415
var value = $field.val();
416+
if (value == '') {
417+
return true;
418+
}
419+
411420
if (options.callback && 'function' == typeof options.callback) {
412421
return options.callback.call(this, value, this);
413422
}
@@ -429,6 +438,9 @@
429438
*/
430439
validate: function(validator, $field, options) {
431440
var value = $field.val();
441+
if (value == '') {
442+
return true;
443+
}
432444

433445
// Accept only digits, dashes or spaces
434446
if (/[^0-9-\s]+/.test(value)) {
@@ -469,8 +481,12 @@
469481
* @returns {Boolean}
470482
*/
471483
validate: function(validator, $field, options) {
472-
var value = $field.val(),
473-
$compareWith = validator.getFieldElement(options.field);
484+
var value = $field.val();
485+
if (value == '') {
486+
return true;
487+
}
488+
489+
var $compareWith = validator.getFieldElement(options.field);
474490
if ($compareWith && value != $compareWith.val()) {
475491
validator.removeError($compareWith);
476492
return true;
@@ -491,7 +507,12 @@
491507
* @returns {Boolean}
492508
*/
493509
validate: function(validator, $field, options) {
494-
return /^\d+$/.test($field.val());
510+
var value = $field.val();
511+
if (value == '') {
512+
return true;
513+
}
514+
515+
return /^\d+$/.test(value);
495516
}
496517
}
497518
}(window.jQuery));
@@ -506,10 +527,14 @@
506527
* @returns {Boolean}
507528
*/
508529
validate: function(validator, $field, options) {
509-
var value = $field.val(),
510-
// Email address regular expression
511-
// http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
512-
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,}))$/;
530+
var value = $field.val();
531+
if (value == '') {
532+
return true;
533+
}
534+
535+
// Email address regular expression
536+
// http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
537+
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,}))$/;
513538
return emailRegExp.test(value);
514539
}
515540
}
@@ -528,7 +553,11 @@
528553
* @returns {Boolean}
529554
*/
530555
validate: function(validator, $field, options) {
531-
var value = parseFloat($field.val());
556+
var value = $field.val();
557+
if (value == '') {
558+
return true;
559+
}
560+
value = parseFloat(value);
532561
return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
533562
}
534563
}
@@ -546,6 +575,9 @@
546575
*/
547576
validate: function(validator, $field, options) {
548577
var value = $field.val();
578+
if (value == '') {
579+
return true;
580+
}
549581
return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
550582
}
551583
};
@@ -562,8 +594,12 @@
562594
* @returns {Boolean}
563595
*/
564596
validate: function(validator, $field, options) {
565-
var value = $field.val(),
566-
$compareWith = validator.getFieldElement(options.field);
597+
var value = $field.val();
598+
if (value == '') {
599+
return true;
600+
}
601+
602+
var $compareWith = validator.getFieldElement(options.field);
567603
if ($compareWith && value == $compareWith.val()) {
568604
validator.removeError($compareWith);
569605
return true;
@@ -587,7 +623,11 @@
587623
* @returns {Boolean}
588624
*/
589625
validate: function(validator, $field, options) {
590-
var value = parseFloat($field.val());
626+
var value = $field.val();
627+
if (value == '') {
628+
return true;
629+
}
630+
value = parseFloat(value);
591631
return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
592632
}
593633
};
@@ -621,6 +661,10 @@
621661
*/
622662
validate: function(validator, $field, options) {
623663
var value = $field.val();
664+
if (value == '') {
665+
return true;
666+
}
667+
624668
return options.regexp.test(value);
625669
}
626670
};
@@ -639,10 +683,15 @@
639683
* <fieldName>: <fieldValue>
640684
* }
641685
* - message: The invalid message
642-
* @returns {String}
686+
* @returns {Boolean|String}
643687
*/
644688
validate: function(validator, $field, options) {
645-
var value = $field.val(), name = $field.attr('name'), data = options.data;
689+
var value = $field.val();
690+
if (value == '') {
691+
return true;
692+
}
693+
694+
var name = $field.attr('name'), data = options.data;
646695
if (data == null) {
647696
data = {};
648697
}
@@ -676,7 +725,12 @@
676725
* @returns {Boolean}
677726
*/
678727
validate: function(validator, $field, options) {
679-
var value = $.trim($field.val()), length = value.length;
728+
var value = $field.val();
729+
if (value == '') {
730+
return true;
731+
}
732+
733+
var length = $.trim(value).length;
680734
if ((options.min && length < options.min) || (options.max && length > options.max)) {
681735
return false;
682736
}
@@ -696,6 +750,11 @@
696750
* @returns {Boolean}
697751
*/
698752
validate: function(validator, $field, options) {
753+
var value = $field.val();
754+
if (value == '') {
755+
return true;
756+
}
757+
699758
// Credit to https://gist.github.com/dperini/729294
700759
//
701760
// Regular Expression for URL validation
@@ -769,7 +828,7 @@
769828
"(?:/[^\\s]*)?" +
770829
"$", "i"
771830
);
772-
return urlExp.test($field.val());
831+
return urlExp.test(value);
773832
}
774833
};
775834
}(window.jQuery));
@@ -785,6 +844,9 @@
785844
*/
786845
validate: function(validateInstance, $field, options) {
787846
var value = $field.val();
847+
if (value == '') {
848+
return true;
849+
}
788850
return /^\d{5}([\-]\d{4})?$/.test(value);
789851
}
790852
};

dist/js/bootstrapValidator.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/js/validator/between.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
* @returns {Boolean}
1414
*/
1515
validate: function(validator, $field, options) {
16-
var value = parseFloat($field.val());
16+
var value = $field.val();
17+
if (value == '') {
18+
return true;
19+
}
20+
21+
value = parseFloat(value);
1722
return (options.inclusive === true)
1823
? (value > options.min && value < options.max)
1924
: (value >= options.min && value <= options.max);

src/js/validator/callback.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
*/
1717
validate: function(validator, $field, options) {
1818
var value = $field.val();
19+
if (value == '') {
20+
return true;
21+
}
22+
1923
if (options.callback && 'function' == typeof options.callback) {
2024
return options.callback.call(this, value, this);
2125
}

src/js/validator/creditCard.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
*/
1313
validate: function(validator, $field, options) {
1414
var value = $field.val();
15+
if (value == '') {
16+
return true;
17+
}
1518

1619
// Accept only digits, dashes or spaces
1720
if (/[^0-9-\s]+/.test(value)) {

src/js/validator/different.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
* @returns {Boolean}
1111
*/
1212
validate: function(validator, $field, options) {
13-
var value = $field.val(),
14-
$compareWith = validator.getFieldElement(options.field);
13+
var value = $field.val();
14+
if (value == '') {
15+
return true;
16+
}
17+
18+
var $compareWith = validator.getFieldElement(options.field);
1519
if ($compareWith && value != $compareWith.val()) {
1620
validator.removeError($compareWith);
1721
return true;

src/js/validator/digits.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
* @returns {Boolean}
1010
*/
1111
validate: function(validator, $field, options) {
12-
return /^\d+$/.test($field.val());
12+
var value = $field.val();
13+
if (value == '') {
14+
return true;
15+
}
16+
17+
return /^\d+$/.test(value);
1318
}
1419
}
1520
}(window.jQuery));

src/js/validator/emailAddress.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@
99
* @returns {Boolean}
1010
*/
1111
validate: function(validator, $field, options) {
12-
var value = $field.val(),
13-
// Email address regular expression
14-
// http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
15-
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,}))$/;
12+
var value = $field.val();
13+
if (value == '') {
14+
return true;
15+
}
16+
17+
// Email address regular expression
18+
// http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
19+
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,}))$/;
1620
return emailRegExp.test(value);
1721
}
1822
}

0 commit comments

Comments
 (0)