victorjonsson/jQuery-Form-Validator — pull request #173 by stevewasiura (2014-08-29 17:01:31+00:00)
Title: numericRangeCheck logic tree fix for default no errors

discovered bug when inputs within range, default return value "OK" not getting returned to caller due to else branch

removed else branch & curly braces.

explained:
input val = 1, allowedrange=min1
logic tree entry will be at line 1034 indexOf min
but value will not be less than minval, so error return is skipped, but the else branch on line 1039 will not be entered, so it is also skipped, and nothing is returned to the caller.

since nothing is returned first, program flow will continue into new line 1039 and func will return array with single element val="ok"

here is expanded function for testing by paste replace into script and look at console.log for debugging:

/**
* Test numeric against allowed range
*
* @param $value int
* @param $rangeAllowed str; (1-2, min1, max2)
* @return array
*/
numericRangeCheck : function($value, $rangeAllowed)
{ console.log('numericRangeCheck inputs: value "'+$value+'", rangeAllowed "' +$rangeAllowed+'"' );
// split by dash
var range = $.split($rangeAllowed, '-');
console.log('range='+range);
// range ?
if (range.length == 2 && ($value < parseInt(range[0],10) || $value > parseInt(range[1],10) ))
{ // value is out of range
console.log('value "'+$value+'", outside of range "' +$rangeAllowed+'"' );
return [ "out", range[0], range[1] ] ;
}
else if ($rangeAllowed.indexOf('min') === 0) // min
{ console.log('range min');
// check if above min
var minQty = parseInt($rangeAllowed.substr(3),10);
console.log(minQty);
if($value < minQty) {
console.log('value "'+$value+'", less than min "' +minQty+'"' );
return [ "min", minQty ] ;
}
}
else if ($rangeAllowed.indexOf('max') === 0) // max
{ console.log('range max');
var maxQty = parseInt($rangeAllowed.substr(3),10);
if($value > maxQty) {
console.log('value "'+$value+'", more than max "' +maxQty+'"' );
return [ "max", maxQty ] ;
}
}

// value is in allowed range
console.log('value "'+$value+'", within range "' +$rangeAllowed+'"' );
return [ "ok" ];
},