| |
|
Converting meter to feet and vice versa |
Both the units ( meter and feet ) are used as unit of length or height or distance in any type of measurement. We can develop one JavaScript application to convert any input to feet or in meter. When it is to be converted to feet then it is assumed that input is meter. Same way when it is converted to meter then it is assumed that entered data is in feet. Note the following conversion formulas.
1 foot = 0.3048 meters
1 meter = 3.2808399 feet
So we only have to multiply the entered value by the respective conversion constant. As we will be using one input box to enter the numeric value, we will be using two buttons one for converting to feet and other one for converting to meter. Here is the demo
Here is the code of above script.
<html>
<head>
<title>Javascript Form Validation</title>
<script language='JavaScript' type='text/JavaScript'>
<!--
function validate(type) {
if(document.form1.textinput.value=='')
{
alert('Fill the Input box before submitting');
return false;
} else {
if(type=="to_feet"){
var res=3.2808*document.form1.textinput.value;
var unit=" feet";
}else{
var res=0.3048*document.form1.textinput.value;
var unit=" meter";
}
document.getElementById("result").innerHTML=res.toFixed(2) + unit;
return false;
}
}
//-->
</script>
</head>
<body>
<form name='form1' action='' method='post'
onSubmit='return validate();'>
Input :<input type=text name=textinput size=10>
<input type=button value='Convert to Feet' onClick=validate('to_feet');>
<input type=button value='Convert to Meter' onClick=validate('to_meter');>
</form>
<div id=result></div>
</body>
</html>
You can add input validation by checking if input is numeric or not.
|
|
|
| Subscribe |
|
Submit your email address and receive
article and product notifications. Your email is safe with us.
|
|
|
|