Created
December 8, 2014 05:42
-
-
Save SeeThruHead/22f514913a439de39504 to your computer and use it in GitHub Desktop.
Throw error if a group of inputs is over a certain sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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