|
386 | 386 | * Short hand function that makes the validation setup require less code
|
387 | 387 | * @param config
|
388 | 388 | */
|
389 |
| - $.setupForm = function(config) { |
| 389 | + $.validatorLoad = function(config) { |
390 | 390 | config = $.extend({
|
391 | 391 | form : 'form',
|
392 | 392 | validateOnBlur : true,
|
|
828 | 828 | $(document).bind("ready", countCharacters);
|
829 | 829 | },
|
830 | 830 |
|
| 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 | + |
831 | 860 | _numSuggestionElements : 0,
|
832 | 861 | _selectedSuggestion : null,
|
833 | 862 | _previousTypedVal : null,
|
|
1043 | 1072 | badTelephone : 'You have not given a correct phone number',
|
1044 | 1073 | badSecurityAnswer : 'You have not given a correct answer to the security question',
|
1045 | 1074 | 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 ', |
1051 | 1079 | notConfirmed : 'Values could not be confirmed',
|
1052 | 1080 | badDomain : 'Incorrect domain value',
|
1053 | 1081 | badUrl : 'The answer you gave was not a correct URL',
|
|
1061 | 1089 | badAlphaNumeric : 'The answer you gave must contain only alphanumeric characters ',
|
1062 | 1090 | badAlphaNumericExtra: ' and ',
|
1063 | 1091 | 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 |
1065 | 1099 | }
|
1066 | 1100 | };
|
1067 | 1101 |
|
|
1198 | 1232 | */
|
1199 | 1233 | $.formUtils.addValidator({
|
1200 | 1234 | 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) { |
1204 | 1238 | var elementType = $el.get(0).nodeName;
|
1205 | 1239 | alert('Please add attribute "data-validation-length" to '+elementType+' named '+$el.attr('name'));
|
1206 | 1240 | return true;
|
1207 | 1241 | }
|
1208 | 1242 |
|
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) ; |
1210 | 1245 |
|
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; |
1230 | 1265 | }
|
1231 |
| - } |
1232 |
| - |
1233 |
| - return true; |
| 1266 | + |
| 1267 | + return checkResult; |
1234 | 1268 | },
|
1235 | 1269 | errorMessage : '',
|
1236 | 1270 | errorMessageKey: ''
|
|
1349 | 1383 | errorMessageKey: 'badDate'
|
1350 | 1384 | });
|
1351 | 1385 |
|
| 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 | + |
1352 | 1456 | })(jQuery);
|
0 commit comments