Skip to content

Commit 3e1013a

Browse files
committed
add validator checkbox_group, plus utility for numeric range
checkbox_group added validator to check a group of checkboxes, to ensure required number of checkboxes were checked, using range (1-2), min1, or max2, etc. lang added prefix & suffixes for checkbox_group renamed prefixes & suffixes used for length validation numericRangeCheck added utility function to evaluate value against allowed parameter. abstracted internal check in validate_length to use this util function instead of repeating functionality vaildate_length edited function to use new utility function numericRangeCheck
1 parent 8fe5113 commit 3e1013a

File tree

1 file changed

+137
-33
lines changed

1 file changed

+137
-33
lines changed

form-validator/jquery.form-validator.js

Lines changed: 137 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@
386386
* Short hand function that makes the validation setup require less code
387387
* @param config
388388
*/
389-
$.setupForm = function(config) {
389+
$.validatorLoad = function(config) {
390390
config = $.extend({
391391
form : 'form',
392392
validateOnBlur : true,
@@ -828,6 +828,35 @@
828828
$(document).bind("ready", countCharacters);
829829
},
830830

831+
/**
832+
* Test numeric against allowed range
833+
*
834+
* @param $value int
835+
* @param $rangeAllowed str; (1-2, min1, max2)
836+
* @return array
837+
*/
838+
numericRangeCheck : function($value, $rangeAllowed)
839+
{
840+
// split by dash
841+
var range = $.split($rangeAllowed, '-');
842+
// range ?
843+
if (range.length == 2 && ($value < parseInt(range[0],10) || $value > parseInt(range[1],10) ))
844+
{ // value is out of range
845+
return [ "out", range[0], range[1] ] ;
846+
}
847+
else if ($rangeAllowed.indexOf('min') === 0) // min
848+
{ // check if above min
849+
var minQty = parseInt($rangeAllowed.substr(3),10);
850+
if($value < minQty) { return [ "min", minQty ] ; }
851+
}
852+
else if ($rangeAllowed.indexOf('max') === 0) // max
853+
{ var maxQty = parseInt($rangeAllowed.substr(3),10);
854+
if($value > maxQty) { return [ "max", maxQty ] ; }
855+
}
856+
else { return "ok"} // value is in allowed range
857+
},
858+
859+
831860
_numSuggestionElements : 0,
832861
_selectedSuggestion : null,
833862
_previousTypedVal : null,
@@ -1043,11 +1072,10 @@
10431072
badTelephone : 'You have not given a correct phone number',
10441073
badSecurityAnswer : 'You have not given a correct answer to the security question',
10451074
badDate : 'You have not given a correct date',
1046-
tooLongStart : 'You have given an answer longer than ',
1047-
tooLongEnd : ' characters',
1048-
tooShortStart : 'You have given an answer shorter than ',
1049-
tooShortEnd : ' characters',
1050-
badLength : 'You have to give an answer between ',
1075+
lengthBadStart : 'You must give an answer between ',
1076+
lengthBadEnd : 'characters',
1077+
lengthTooLongStart : 'You have given an answer longer than ',
1078+
lengthTooShortStart : 'You have given an answer shorter than ',
10511079
notConfirmed : 'Values could not be confirmed',
10521080
badDomain : 'Incorrect domain value',
10531081
badUrl : 'The answer you gave was not a correct URL',
@@ -1061,7 +1089,13 @@
10611089
badAlphaNumeric : 'The answer you gave must contain only alphanumeric characters ',
10621090
badAlphaNumericExtra: ' and ',
10631091
wrongFileSize : 'The file you are trying to upload is too large',
1064-
wrongFileType : 'The file you are trying to upload is of wrong type'
1092+
wrongFileType : 'The file you are trying to upload is of wrong type',
1093+
groupCheckedRangeStart : 'Please choose between ',
1094+
groupCheckedTooFewStart : 'Please choose at least ',
1095+
groupCheckedTooManyStart : 'Please choose a maximum of ',
1096+
groupCheckedEnd : ' item(s)',
1097+
1098+
_dummy--last-item-placeholder-without-comma : 0
10651099
}
10661100
};
10671101

@@ -1198,39 +1232,39 @@
11981232
*/
11991233
$.formUtils.addValidator({
12001234
name : 'length',
1201-
validate : function(value, $el, config, language) {
1202-
var len = $el.valAttr('length');
1203-
if(len == undefined) {
1235+
validate : function(value, $el, config, lang) {
1236+
var lengthAllowed = $el.valAttr('length');
1237+
if(lengthAllowed == undefined) {
12041238
var elementType = $el.get(0).nodeName;
12051239
alert('Please add attribute "data-validation-length" to '+elementType+' named '+$el.attr('name'));
12061240
return true;
12071241
}
12081242

1209-
var range = $.split(len, '-');
1243+
// check if length is above min, below max, within range etc.
1244+
var lengthCheckResults = $.formUtils.numericRangeCheck(value.length, lengthAllowed) ;
12101245

1211-
// range
1212-
if(range.length == 2 && (value.length < parseInt(range[0],10) || value.length > parseInt(range[1],10))) {
1213-
this.errorMessage = language.badLength + len + language.tooLongEnd;
1214-
return false;
1215-
}
1216-
// min
1217-
else if(len.indexOf('min') === 0) {
1218-
var minLength = parseInt(len.substr(3),10);
1219-
if(minLength > value.length) {
1220-
this.errorMessage = language.tooShortStart + minLength + language.tooShortEnd;
1221-
return false;
1222-
}
1223-
}
1224-
// max
1225-
else if(len.indexOf('max') === 0) {
1226-
var maxLength = parseInt(len.substr(3),10);
1227-
if(maxLength < value.length) {
1228-
this.errorMessage = language.tooLongStart + maxLength + language.tooLongEnd;
1229-
return false;
1246+
switch(lengthCheckResults[0] )
1247+
{ // outside of allowed range
1248+
case "out":
1249+
this.errorMessage = lang.lengthBadStart + len + lang.lengthBadEnd;
1250+
checkResult = false;
1251+
break;
1252+
// too short
1253+
case "min":
1254+
this.errorMessage = lang.lengthTooShortStart + lengthCheckResults[1] + lang.lengthBadEnd;
1255+
checkResult = false;
1256+
break;
1257+
// too long
1258+
case "max":
1259+
this.errorMessage = lang.lengthTooLongStart + lengthCheckResults[1] + lang.lengthBadEnd;
1260+
checkResult = false;
1261+
break;
1262+
// ok
1263+
default:
1264+
checkResult = true;
12301265
}
1231-
}
1232-
1233-
return true;
1266+
1267+
return checkResult;
12341268
},
12351269
errorMessage : '',
12361270
errorMessageKey: ''
@@ -1349,4 +1383,74 @@
13491383
errorMessageKey: 'badDate'
13501384
});
13511385

1386+
1387+
/*
1388+
* Validate group of checkboxes, validate qty required is checked
1389+
* written by Steve Wasiura : http://stevewasiura.waztech.com
1390+
* element attrs
1391+
* data-validation="checkbox_group"
1392+
* data-validation-qty="1-2" // min 1 max 2
1393+
* data-validation-error-msg="chose min 1, max of 2 checkboxes"
1394+
*/
1395+
1396+
/* formUtils global var to hold string of checkboxes that were prev checked by validator,
1397+
* to prevent wasted duplication when multiple checkboxes have call to same validator
1398+
*/
1399+
$.formUtils._checkboxGroups = '';
1400+
1401+
$.formUtils.addValidator({
1402+
name : 'checkbox_group',
1403+
validate : function(val, $el, config, lang, form)
1404+
{ // set return var
1405+
var checkResult = true;
1406+
// get name of element. since it is a checkbox group, all checkboxes will have same name
1407+
var elname = $el.attr('name');
1408+
// check if we have already checked this group
1409+
// global var "_checkboxGroups"
1410+
// if element name is not found in global var, then do the check
1411+
if ($.formUtils._checkboxGroups.indexOf(elname) == -1 )
1412+
{ // get count of checked checkboxes with this name
1413+
var checkedCount = $("input[type=checkbox][name^='"+elname+"']:checked", form).length;
1414+
// get el attr that specs qty required / allowed
1415+
var qtyAllowed = $el.valAttr('qty');
1416+
if (qtyAllowed == undefined) {
1417+
var elementType = $el.get(0).nodeName;
1418+
alert('Attribute "data-validation-qty" is missing from '+elementType+' named '+$el.attr('name'));
1419+
}
1420+
// call Utility function to check if count is above min, below max, within range etc.
1421+
var qtyCheckResults = $.formUtils.numericRangeCheck(checkedCount, qtyAllowed) ;
1422+
// results will be array, [0]=result str, [1]=qty int
1423+
switch(qtyCheckResults[0] )
1424+
{ // outside allowed range
1425+
case "out":
1426+
this.errorMessage = lang.groupCheckedRangeStart + qtyAllowed + lang.groupCheckedEnd;
1427+
checkResult = false;
1428+
break;
1429+
// below min qty
1430+
case "min":
1431+
this.errorMessage = lang.groupCheckedTooFewStart + qtyCheckResults[1] + lang.groupCheckedEnd;
1432+
checkResult = false;
1433+
break;
1434+
// above max qty
1435+
case "max":
1436+
this.errorMessage = lang.groupCheckedTooManyStart + qtyCheckResults[1] + lang.groupCheckedEnd;
1437+
checkResult = false;
1438+
break;
1439+
// ok
1440+
default:
1441+
checkResult = true;
1442+
}
1443+
1444+
// add element name to global var so group won't be checked on subsequent calls
1445+
$.formUtils._checkboxGroups += elname + ', ';
1446+
}
1447+
1448+
return checkResult;
1449+
1450+
} // remove comma
1451+
// errorMessage : '', // set above in switch statement
1452+
// errorMessageKey: '' // not used
1453+
});
1454+
1455+
13521456
})(jQuery);

0 commit comments

Comments
 (0)