How to limit values for oninput calculation without limiting the input values?

How can one limit the output values, say, from 5 to 195. without limiting the input values?

<!DOCTYPE html>
<html>
<body>

<form oninput="x.value=parseInt(a.value)+parseInt(b.value)">0
<input type="range" id="a" value="50">100 +
<input type="number" id="b" value="50">=
<output name="x" for="a b"></output>
</form>

</body>
</html>

Answers


<!DOCTYPE html>
<html>
<body>

<form oninput="var n = parseInt(a.value)+parseInt(b.value); x.value =  n < 5 ? 5 : ( n > 195 ? 195 : n);">0
<input type="range" id="a" value="50">100 +
<input type="number" id="b" value="50">=
<output name="x" for="a b"></output>
</form>

</body>
</html>

This uses a nested trinary opertor a ? b : c which is essentially a if b else c. When it's nested you can think of it like this: a ? b : (c ? d : e) as a if b ELSE (c if d else e) as the result of (c ? d : e) is the equivalent of c in a ? b : c.

Try it out here

Alternativley, you could do

<form oninput="x.value = Math.min(Math.max(5,parseInt(a.value)+parseInt(b.value)),195);"/>

Which achieves the same result, just in a different way, you can play with it here

Try it out here: JSfiddle


Need Your Help

After subraction of 4 neighbor pixels always give zero

c++ opencv

I am trying to take difference of center pixel with 4 neighbor and add them and then replace the original with that difference value. but it always replace pixel with zero. I don't what i am doing ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.