Skip to content

Instantly share code, notes, and snippets.

@SeeThruHead
Created December 8, 2014 05:42
Show Gist options
  • Save SeeThruHead/22f514913a439de39504 to your computer and use it in GitHub Desktop.
Save SeeThruHead/22f514913a439de39504 to your computer and use it in GitHub Desktop.
Throw error if a group of inputs is over a certain sum
function checkSum(groupOfInputs, maxValue) {
groupOfInputs = $(groupOfInputs);
groupOfInputs.on('change keyup paste', function() {
$this = $(this);
var values = groupOfInputs.map(function() {return $(this).val();}).get();
var sum = values.reduce(function(one, two) {
return Number(one) + Number(two);
});
if (sum > maxValue) {
alert('Too big!');
$this.val('').focus();
}
});
}
var checkSumThous = function(groupOfInputs) {
return checkSum(groupOfInputs, 1000);
};
checkSum('.one', 1000);
checkSum('.two', 500);
checkSumThous('.three');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment