Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

please enter a valid value the two nearest valid value is ?

my assignment is to make weight calculator but due to limitations of university i am not able to use all tags so i write code it works fine but gives me a error of ( please enter a valid value the two nearest valid value are number and number )

here is code

<html>
<head>
<title>Weight Convertor Calculator</title>
<script>
var z;
var x;
function converter(){ 
if(document.weight.kg.value>=1)
{
x=document.weight.kg.value;
document.getElementById("g").value=x*1000;
document.getElementById("p").value=x*2.2046;
document.getElementById("m").value=x*1000000;
document.getElementById("ut").value=x*0.0011023;
z=35.274*x;
document.getElementById("o").value=z;

}
else
{
 window.alert("please enter any number greater than 0");
  }
 }
 </script>
 </head>

 <body bgcolor="#b3f0ff">
 <h1 align="center" style="color:#ff0066;"> Weight Calculator</h1>
 <br>
 <br>
 <form name="weight" method="post">
 <table align="center" style="color:#ff0066;">
 <tr>
 <td><b>Enter your weight in Kg:</b></td>
 <td><input type="number" id="kg"  name="kilogram" 
 placeholder="enter_any_number"></td>
 </tr>
 <tr>
 <td align="right"><input type="submit" value="Convert" 
 onClick="converter(kg.value)"></td>
 <td><input type="reset"></td>
 </tr>
 <tr>
 <td align="right"><b>weight in Grams=</b></td>
 <td><input type="number" id="g" name="gram"></td>
 </tr>
 <tr>
 <td align="right"><b>weight in Pounds=</b></td>
 <td><input type="text" id="p" name="pound"></td> 
 </tr>
 <tr>
 <td align="right"><b>weight in MilliGrams=</b></td>
 <td><input type="number" id="m" name="milligram"></td>
 </tr>
 <tr>
 <td align="right"><b>weight in US Ton=</b></td>
 <td><input type="text" id="ut" name="uton"></td>
 </tr>
 <tr>
 <td align="right"><b>weight in Ounces=</b></td>
 <td><input type="number" id="o" name="ounce" ></td>
 </tr>
 </table> 
 </form><br>
 <p align="center" style="color:#ff0066;"><b>NOTE: Enter only numerical 
  value which is greater than 0</b></p>
  </body>
  </html>

i think that is because of number attribute in input tag but i can not able to find whats wrong and how to accurate it. there is one more error if i only calculate one convertion output flash on screen and vanished if i get output in all fields then answer is shown.

like image 227
shoaib sabir Avatar asked Feb 20 '18 19:02

shoaib sabir


2 Answers

Try adding the attribute step="any" to your input. That helped me.

like image 191
Jeremy Tammik Avatar answered Sep 19 '22 11:09

Jeremy Tammik


1.) In number inputs fragments are not allowed.
So you need to use an text input
2.) Your form will be submitted: data gone
Here you can find a demo that works, based on your code.

like image 28
bru02 Avatar answered Sep 20 '22 11:09

bru02