The logic works like this, considering that there are 3 users:
If user A is adding value, he will have to add the existing value of B and C to see what's left over for him.
If it is B, add A and C.
And if it's C, add A and B.
Now if it's a single user with three input fields, we'll just use JAVASCRIPT.
It will not work in Internet Explorer because I used input
of type number
to change the MAX
attribute of each field:
function distribuirPercent(t){
var myform = document.forms.porcentagem;
var lmt;
var n = t.name;
/* Pré limitação por segurança */
myform.campo_a.max = myform.campo_a.value;
myform.campo_b.max = myform.campo_b.value;
myform.campo_c.max = myform.campo_c.value;
/* ---------------------------------------------*/
/* Transformar campos em números para operações */
var a = parseInt(myform.campo_a.value);
var b = parseInt(myform.campo_b.value);
var c = parseInt(myform.campo_c.value);
/* ---------------------------------------------*/
if(n == "campo_a"){
lmt = b+c;
}
if(n == "campo_b"){
lmt = a+c;
}
if(n == "campo_c"){
lmt = a+b;
}
console.clear();
console.log("Total dos outros 2: "+lmt);
lmt = 100-lmt;
console.log("Limite permitido: "+lmt);
t.max = lmt;
}
input[type=number] {
-webkit-appearance: textfield;
-moz-appearance: textfield;
appearance: textfield;
margin: 0;
border: solid 0px #fff;
width: 20px;
text-align: right;
}
<form name="porcentagem">
<label><input type="number" name="campo_a" min="0" max="100" value="0" onfocus="distribuirPercent(this)" size="3" />%</label><br />
<label><input type="number" name="campo_b" min="0" max="100" value="0" onfocus="distribuirPercent(this)" size="3" />%</label><br />
<label><input type="number" name="campo_c" min="0" max="100" value="0" onfocus="distribuirPercent(this)" size="3" />%</label>
</form>