How do I convert number field data to the same format?
I have a form that contains a calculation for the resistance of a type of product. For example, if you choose the product that has 350W, it calculates the resistance. 52900 / 350 = Resistance. When this value is calculated, it ends up in a field called resistance #input_1_22
. We use this field to calculate two new values. A min value and a max value.
Min Resistance (-5%) = Resistance * 0.95 #input_1_23
Max resistance (+10%) = Resistance * 1.1 #input_1_24
When these fields are calculated, it is the user's turn to fill in the value of their own installation, so that it lies between min and max resistance #input_1_25
If the value is approved true
, the box turns green, span#result1
is filled with a value and span#result2
becomes empty.
If the value is not approved false
, the box turns red, span#result2
is filled with a value and span#result1
becomes empty.
To calculate if the value is passed, I have the following code:
<script>
// When the field is changed do function
document.getElementById("input_1_25").addEventListener("input", myFunction);
function myFunction() {
var x = document.getElementById("input_1_23").value; // Collect my value from input
var y = document.getElementById("input_1_24").value; // Collect max value from input
$value = document.getElementById("input_1_25").value;
if (($value >= x) && ($value <= y)) {
document.getElementById("result1").innerHTML = "✓";
document.getElementById("result2").innerHTML = "";
input = document.getElementById('input_1_25');
input.style.borderColor = 'green';
} else {
document.getElementById("result2").innerHTML = "X";
document.getElementById("result1").innerHTML = "";
input = document.getElementById('input_1_25');
input.style.borderColor = 'red';
}
}
</script>
It works on most values, but not all. If I choose, for example, 550W, it calculates:
value: 96.18
min value: 91.37
max value: 105.8
Input: 96 = false, why?
Then I should get true
if I fill in a value of 96.
But whatever I enter in user input #input_1_25
, it becomes false
.
I assume it has to do with the values I get out being from different data? Maybe comparing a string with a number? So that "2" < "12" = false?
If so, how do I get the values to be the same type?