Skip to content

Commit cbced2c

Browse files
committed
moved length validating functions into one
1 parent e9defcf commit cbced2c

13 files changed

+94
-109
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
**jQuery Form Validator* is a feature rich jQuery plugin that makes it easy to validate user input in HTML forsm
2+
**jQuery Form Validator** is a feature rich jQuery plugin that makes it easy to validate user input in HTML forsm
33
while keeping the HTML code clean from javascript logic. Even though the plugin has **a wide range of validation functions**
44
it's designed to consume as little bandwidth as possible (which makes it very suitable for **mobile websites**).
55
This is achieved by grouping together validation functions in "modules", making it possible for the programmer
@@ -332,6 +332,15 @@ $_SESSION['captcha'] = array( mt_rand(0,9), mt_rand(1, 9) );
332332
<p>Confirm password: <input type="password" name="pass_confirmation" /></p>
333333
```
334334

335+
## Changelog
336+
337+
### 2.0
338+
* validate_[min|max]_length is removed (now merged with validate_length)
339+
* validate_number, validate_int, validate_float is merged together, all three variants is now validated by validate_number
340+
* validate_phone moved to "sweden" module and renamed to validate_swephone
341+
* The attribute to be used when defining the regular expression for validate_custom is now moved to its own attribute
342+
* validate_length now looks at attribute data-validation-length to find out how long or short the value must be
343+
335344
## Contributors
336345
[Joel Sutherland](https://github.com/robamaton) (contributor)<br />
337346
[Steve Wasiura](https://github.com/stevewasiura) (contributor)<br />

js/formvalidator/date.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*
1313
*
1414
* @license Dual licensed under the MIT or GPL Version 2 licenses
15-
* @version 1.9.4
15+
* @version 1.9.7
1616
*/
1717
(function($) {
1818

js/formvalidator/date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
*
1313
*
1414
* @license Dual licensed under the MIT or GPL Version 2 licenses
15-
* @version 1.9.4
15+
* @version 1.9.7
1616
*/(function(a){a.formUtils.addValidator({name:"validate_time",validate:function(a){if(a.match(/^(\d{2}):(\d{2})$/)===null)return!1;var b=parseInt(a.split(":")[0],10),c=parseInt(a.split(":")[1],10);return b>24||c>59||b===24&&c>0?!1:!0},errorMessage:"",errorMessageKey:"badTime"}),a.formUtils.addValidator({name:"validate_birthdate",validate:function(b,c,d){var e="yyyy-mm-dd";c.attr("data-format")?e=c.attr("data-format"):typeof d.dateFormat!="undefined"&&(e=d.dateFormat);var f=a.formUtils.parseDate(b,e);if(!f)return!1;var g=new Date,h=g.getFullYear(),i=f[0],j=f[1],k=f[2];if(i===h){var l=g.getMonth()+1;if(j===l){var m=g.getDate();return k<=m}return j<l}return i<h&&i>h-124},errorMessage:"",errorMessageKey:"badDate"})})(jQuery);

js/formvalidator/jquery.formvalidator.js

Lines changed: 45 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Documentation and issue tracking on Github <https://github.com/victorjonsson/jQuery-Form-Validator/>
66
*
77
* @license Dual licensed under the MIT or GPL Version 2 licenses
8-
* @version 1.9.4
8+
* @version 1.9.7
99
*/
1010
(function($) {
1111

@@ -413,7 +413,6 @@
413413
var loadModuleScripts = function(modules, path) {
414414
$.each(modules.split(','), function(i, module) {
415415
var scriptUrl = path + $.trim(module) + '.js';
416-
var doCache = scriptUrl.substr(-7) != '.dev.js';
417416
$.ajax({
418417
url : scriptUrl,
419418
cache : scriptUrl.substr(-7) != '.dev.js',
@@ -1004,55 +1003,39 @@
10041003
errorMessageKey: 'requiredFields'
10051004
});
10061005

1007-
/*
1008-
* Validate min length
1009-
*/
1010-
$.formUtils.addValidator({
1011-
name : 'validate_min_length',
1012-
validate : function(value, $el, config, language) {
1013-
var validationRules = $el.attr(config.validationRuleAttribute);
1014-
var minLength = $.formUtils.getAttributeInteger(validationRules, 'length');
1015-
if (value.length < minLength) {
1016-
this.errorMessage = language.toShortStart + minLength + language.toShortEnd;
1017-
return false;
1018-
}
1019-
return true;
1020-
},
1021-
errorMessage : '',
1022-
errorMessageKey: ''
1023-
});
1024-
1025-
/*
1026-
* Validate max length
1027-
*/
1028-
$.formUtils.addValidator({
1029-
name : 'validate_max_length',
1030-
validate : function(value, $el, config, language) {
1031-
var validationRules = $el.attr(config.validationRuleAttribute);
1032-
var maxLength = $.formUtils.getAttributeInteger(validationRules, 'length');
1033-
if (value.length > maxLength) {
1034-
this.errorMessage = language.toLongStart + maxLength + language.toLongEnd;
1035-
return false;
1036-
}
1037-
return true;
1038-
},
1039-
errorMessage : '',
1040-
errorMessageKey: ''
1041-
});
1042-
10431006
/*
10441007
* Validate length range
10451008
*/
10461009
$.formUtils.addValidator({
10471010
name : 'validate_length',
10481011
validate : function(value, $el, config, language) {
1049-
var validationRules = $el.attr(config.validationRuleAttribute);
1050-
var lengthRange = $.formUtils.getAttributeInteger(validationRules, 'length');
1051-
var range = lengthRange.split('-');
1052-
if (value.length < parseInt(range[0],10) || value.length > parseInt(range[1],10)) {
1053-
this.errorMessage = language.badLength + lengthRange + language.toLongEnd;
1012+
var len = $el.attr('data-validation-length');
1013+
var range = len.split('-');
1014+
1015+
// range
1016+
if(range.length == 2 && (value.length < parseInt(range[0],10) || value.length > parseInt(range[1],10))) {
1017+
this.errorMessage = language.badLength + len + language.toLongEnd;
10541018
return false;
10551019
}
1020+
else if(len.indexOf('min') === 0) {
1021+
var minLength = parseInt(len.substr(3),10);
1022+
if(minLength > value.length) {
1023+
this.errorMessage = language.toShortStart + minLength + language.toShortEnd;
1024+
return false;
1025+
}
1026+
}
1027+
else if(len.indexOf('max') === 0) {
1028+
var maxLength = parseInt(len.substr(3),10);
1029+
if(maxLength < value.length) {
1030+
this.errorMessage = language.toLongStart + maxLength + language.toLongEnd;
1031+
return false;
1032+
}
1033+
}
1034+
else {
1035+
var elementType = $el.get(0).nodeName;
1036+
alert('Please add attribute "data-validation-length" to '+elementType+' named '+$el.attr('name'));
1037+
}
1038+
10561039
return true;
10571040
},
10581041
errorMessage : '',
@@ -1065,7 +1048,7 @@
10651048
$.formUtils.addValidator({
10661049
name : 'validate_url',
10671050
validate : function(url) {
1068-
// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/ but added support for arrays in the url ?arg[]=sdfsdf
1051+
// written by Scott Gonzalez: http://projects.scottsplayground.com/iri/ but added support for arrays in the url ?arg[]=sdfsdf
10691052
var urlFilter = /^(https|http|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|\[|\]|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i;
10701053
if(urlFilter.test(url)) {
10711054
var domain = url.split(/^https|^http|^ftp/i)[1].replace('://', '');
@@ -1086,8 +1069,23 @@
10861069
*/
10871070
$.formUtils.addValidator({
10881071
name : 'validate_number',
1089-
validate : function(val) {
1090-
return $.formUtils.validators.validate_int.validate(val) || $.formUtils.validators.validate_float.validate(val);
1072+
validate : function(val, $el) {
1073+
if(val !== '') {
1074+
var allowing = $el.attr('data-validation-allowing');
1075+
if(allowing === undefined)
1076+
allowing = 'number';
1077+
1078+
if(allowing.indexOf('negative') > -1 && val.indexOf('-') === 0)
1079+
val = val.substr(1);
1080+
1081+
if(allowing.indexOf('number') > -1 && val.replace(/[0-9]/g, '') === '') {
1082+
return true;
1083+
}
1084+
if(allowing.indexOf('float') > -1 && val.match(/^([0-9]+)\.([0-9]+)$/) !== null) {
1085+
return true;
1086+
}
1087+
}
1088+
return false;
10911089
},
10921090
errorMessage : '',
10931091
errorMessageKey: 'badInt'
@@ -1099,38 +1097,13 @@
10991097
$.formUtils.addValidator({
11001098
name : 'validate_custom',
11011099
validate : function(val, $el, config) {
1102-
var attr = $el.attr(config.validationRuleAttribute);
1103-
var regexp = new RegExp(attr.split('regexp/')[1].split('/')[0]);
1100+
var regexp = new RegExp($el.attr('data-validation-regexp'));
11041101
return regexp.test(val);
11051102
},
11061103
errorMessage : '',
11071104
errorMessageKey: 'badCustomVal'
11081105
});
11091106

1110-
/*
1111-
* Validate integer
1112-
*/
1113-
$.formUtils.addValidator({
1114-
name : 'validate_int',
1115-
validate : function(val) {
1116-
return val !== '' && val.replace(/[0-9]/g, '') === '';
1117-
},
1118-
errorMessage : '',
1119-
errorMessageKey: 'badInt'
1120-
});
1121-
1122-
/*
1123-
* Validate floating number
1124-
*/
1125-
$.formUtils.addValidator({
1126-
name : 'validate_float',
1127-
validate : function(val) {
1128-
return val.match(/^(\-|)([0-9]+)\.([0-9]+)$/) !== null;
1129-
},
1130-
errorMessage : '',
1131-
errorMessageKey: 'badFloat'
1132-
});
1133-
11341107
/*
11351108
* Validate date
11361109
*/
@@ -1151,27 +1124,4 @@
11511124
errorMessageKey: 'badDate'
11521125
});
11531126

1154-
/*
1155-
* Validate phone number, at least 7 digits only one hyphen and plus allowed
1156-
*/
1157-
$.formUtils.addValidator({
1158-
name : 'validate_phone',
1159-
validate : function(tele) {
1160-
var numPlus = tele.match(/\+/g);
1161-
var numHifen = tele.match(/-/g);
1162-
1163-
if ((numPlus !== null && numPlus.length > 1) || (numHifen !== null && numHifen.length > 1)) {
1164-
return false;
1165-
}
1166-
if (numPlus !== null && tele.indexOf('+') !== 0) {
1167-
return false;
1168-
}
1169-
1170-
tele = tele.replace(/([-|\+])/g, '');
1171-
return tele.length > 8 && tele.match(/[^0-9]/g) === null;
1172-
},
1173-
errorMessage : '',
1174-
errorMessageKey: 'badTelephone'
1175-
});
1176-
11771127
})(jQuery);

js/formvalidator/jquery.formvalidator.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/formvalidator/location.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
*
1313
* @license Dual licensed under the MIT or GPL Version 2 licenses
14-
* @version 1.9.4
14+
* @version 1.9.7
1515
*/
1616
(function($) {
1717

js/formvalidator/location.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.

js/formvalidator/security.dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* - validate_backend
1313
*
1414
* @license Dual licensed under the MIT or GPL Version 2 licenses
15-
* @version 1.9.4
15+
* @version 1.9.7
1616
*/
1717
(function($) {
1818

0 commit comments

Comments
 (0)