Skip to content

Commit 6400635

Browse files
committed
bug fix numericRangeCheck, bug fix checkbox_group
numericRangeCheck had bug in conditional structure; fixed. checkbox_group had bug when checking checkboxes in same group. added special test in validateInput() function, to force element to be first input element in group, so error msg only output once. also changed .append back to .text to support this case.
1 parent d415c7d commit 6400635

File tree

1 file changed

+49
-62
lines changed

1 file changed

+49
-62
lines changed

form-validator/jquery.form-validator.js

Lines changed: 49 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@
312312
var $parent = $input.parent(),
313313
$errorSpan = $parent.find('span[class='+config.errorMessageClass+']');
314314
if ($errorSpan.length > 0) {
315-
$errorSpan.append(', '+$input.valAttr('current-error'));
315+
$errorSpan.text(', '+$input.valAttr('current-error'));
316316
} else {
317317
$parent.append('<span class="'+config.errorMessageClass+'">' + $input.valAttr('current-error') + '</span>');
318318
}
@@ -697,6 +697,11 @@
697697
var validator = $.formUtils.validators[rule];
698698

699699
if( validator && typeof validator['validatorFunction'] == 'function' ) {
700+
// special change of element for checkbox_group rule
701+
if ( rule == 'validate_checkbox_group' ) {
702+
// set element to first in group, so error msg is set only once
703+
$element = $("[name='"+$element.attr('name')+"']:eq(0)");
704+
}
700705

701706
var isValid = validator.validatorFunction(value, $element, config, language, $form);
702707

@@ -836,25 +841,20 @@
836841
* @param $rangeAllowed str; (1-2, min1, max2)
837842
* @return array
838843
*/
839-
numericRangeCheck : function($value, $rangeAllowed)
844+
numericRangeCheck : function(value, rangeAllowed)
840845
{
841846
// split by dash
842-
var range = $.split($rangeAllowed, '-');
847+
var range = $.split(rangeAllowed, '-');
848+
// min or max
849+
var minmax = parseInt(rangeAllowed.substr(3),10)
843850
// range ?
844-
if (range.length == 2 && ($value < parseInt(range[0],10) || $value > parseInt(range[1],10) ))
845-
{ // value is out of range
846-
return [ "out", range[0], range[1] ] ;
847-
}
848-
else if ($rangeAllowed.indexOf('min') === 0) // min
849-
{ // check if above min
850-
var minQty = parseInt($rangeAllowed.substr(3),10);
851-
if($value < minQty) { return [ "min", minQty ] ; }
852-
}
853-
else if ($rangeAllowed.indexOf('max') === 0) // max
854-
{ var maxQty = parseInt($rangeAllowed.substr(3),10);
855-
if($value > maxQty) { return [ "max", maxQty ] ; }
856-
}
857-
else { return "ok"} // value is in allowed range
851+
if (range.length == 2 && (value < parseInt(range[0],10) || value > parseInt(range[1],10) ) )
852+
{ return [ "out", range[0], range[1] ] ; } // value is out of range
853+
else if (rangeAllowed.indexOf('min') === 0 && (value < minmax ) ) // min
854+
{ return ["min", minmax]; } // value is below min
855+
else if (rangeAllowed.indexOf('max') === 0 && (value > minmax ) ) // max
856+
{ return ["max", minmax]; } // value is above max
857+
else { return [ "ok" ] ; } // value is in allowed range
858858
},
859859

860860

@@ -1394,56 +1394,43 @@
13941394
* data-validation-error-msg="chose min 1, max of 2 checkboxes"
13951395
*/
13961396

1397-
/* formUtils global var to hold string of checkboxes that were prev checked by validator,
1398-
* to prevent wasted duplication when multiple checkboxes have call to same validator
1399-
*/
1400-
$.formUtils._checkboxGroups = '';
1401-
14021397
$.formUtils.addValidator({
14031398
name : 'checkbox_group',
1404-
validatorFunction : function(val, $el, config, lang, form)
1405-
{ // set return var
1399+
validatorFunction : function(val, $el, config, lang, $form)
1400+
{ // preset return var
14061401
var checkResult = true;
14071402
// get name of element. since it is a checkbox group, all checkboxes will have same name
14081403
var elname = $el.attr('name');
1409-
// check if we have already checked this group
1410-
// global var "_checkboxGroups"
1411-
// if element name is not found in global var, then do the check
1412-
if ($.formUtils._checkboxGroups.indexOf(elname) == -1 )
1413-
{ // get count of checked checkboxes with this name
1414-
var checkedCount = $("input[type=checkbox][name^='"+elname+"']:checked", form).length;
1415-
// get el attr that specs qty required / allowed
1416-
var qtyAllowed = $el.valAttr('qty');
1417-
if (qtyAllowed == undefined) {
1418-
var elementType = $el.get(0).nodeName;
1419-
alert('Attribute "data-validation-qty" is missing from '+elementType+' named '+$el.attr('name'));
1420-
}
1421-
// call Utility function to check if count is above min, below max, within range etc.
1422-
var qtyCheckResults = $.formUtils.numericRangeCheck(checkedCount, qtyAllowed) ;
1423-
// results will be array, [0]=result str, [1]=qty int
1424-
switch(qtyCheckResults[0] )
1425-
{ // outside allowed range
1426-
case "out":
1427-
this.errorMessage = lang.groupCheckedRangeStart + qtyAllowed + lang.groupCheckedEnd;
1428-
checkResult = false;
1429-
break;
1430-
// below min qty
1431-
case "min":
1432-
this.errorMessage = lang.groupCheckedTooFewStart + qtyCheckResults[1] + lang.groupCheckedEnd;
1433-
checkResult = false;
1434-
break;
1435-
// above max qty
1436-
case "max":
1437-
this.errorMessage = lang.groupCheckedTooManyStart + qtyCheckResults[1] + lang.groupCheckedEnd;
1438-
checkResult = false;
1439-
break;
1440-
// ok
1441-
default:
1442-
checkResult = true;
1443-
}
1444-
1445-
// add element name to global var so group won't be checked on subsequent calls
1446-
$.formUtils._checkboxGroups += elname + ', ';
1404+
// get count of checked checkboxes with this name
1405+
var checkedCount = $("input[type=checkbox][name^='"+elname+"']:checked", $form).length;
1406+
// get el attr that specs qty required / allowed
1407+
var qtyAllowed = $el.valAttr('qty');
1408+
if (qtyAllowed == undefined) {
1409+
var elementType = $el.get(0).nodeName;
1410+
alert('Attribute "data-validation-qty" is missing from '+elementType+' named '+$el.attr('name'));
1411+
}
1412+
// call Utility function to check if count is above min, below max, within range etc.
1413+
var qtyCheckResults = $.formUtils.numericRangeCheck(checkedCount, qtyAllowed) ;
1414+
// results will be array, [0]=result str, [1]=qty int
1415+
switch(qtyCheckResults[0] ) {
1416+
// outside allowed range
1417+
case "out":
1418+
this.errorMessage = lang.groupCheckedRangeStart + qtyAllowed + lang.groupCheckedEnd;
1419+
checkResult = false;
1420+
break;
1421+
// below min qty
1422+
case "min":
1423+
this.errorMessage = lang.groupCheckedTooFewStart + qtyCheckResults[1] + lang.groupCheckedEnd;
1424+
checkResult = false;
1425+
break;
1426+
// above max qty
1427+
case "max":
1428+
this.errorMessage = lang.groupCheckedTooManyStart + qtyCheckResults[1] + lang.groupCheckedEnd;
1429+
checkResult = false;
1430+
break;
1431+
// ok
1432+
default:
1433+
checkResult = true;
14471434
}
14481435

14491436
return checkResult;

0 commit comments

Comments
 (0)