Akilesh Khoday
Akilesh Khoday

Reputation: 51

Cannot get the output

 <html>
    <head>
    <script type="text/javascript">
    function checkunit()
    {
    if(document.getElementById("unit2").value=="CK")
    {
    document.getElementById("unittype1").innerHTML='cm';
    document.getElementById("unittype2").innerHTML='cm';
    document.getElementById("unittype3").innerHTML='cm';
    }
    else
    {
    document.getElementById("unittype1").innerHTML='in';
    document.getElementById("unittype2").innerHTML='in';
    document.getElementById("unittype3").innerHTML='in';
    }
    }
    function validatecal(form)
    {
    if (form.vm_length.value=="" || form.vm_length.value==0) {alert('Enter Length'); form.vm_length.focus(); return false}
    if (!IsNumber(form.vm_length.value) && form.vm_length.value!="") {alert('Invalid Length'); form.vm_length.focus(); return false}
    if (form.vm_width.value=="" || form.vm_width.value==0) {alert('Enter Width'); form.vm_width.focus(); return false}
    if (!IsNumber(form.vm_width.value) && form.vm_width.value!="") {alert('Invalid Width'); form.vm_width.focus(); return false}
    if (form.vm_height.value=="" || form.vm_height.value==0) {alert('Enter Height'); form.vm_height.focus(); return false}
    if (!IsNumber(form.vm_height.value) && form.vm_height.value!="") {alert('Invalid Height'); form.vm_height.focus(); return false}
    calcVolWeight();
    return false;
    }

    function calcVolWeight() {
            var lengthValue = document.getElementById("vm_length").value;
            var widthValue = document.getElementById("vm_width").value;
            var heightValue = document.getElementById("vm_height").value;
            var weightDivisor = document.getElementById("unit2").value == "IP" ? 139 : 5000;
            var roundFactor = document.getElementById("unit2").value == "IP" ? 1 : 0.5; 
             var originalVolume = lengthValue * widthValue * heightValue;
                    // Calculate the volumetric weight
                    var calcVolWeight = roundFactor * Math.ceil(( originalVolume / weightDivisor ) / roundFactor);
                     document.getElementById("total_weight").value = parseFloat(calcVolWeight);
            }

      </script>

    </head>
    <body>
      <form name="vcalculator" id="vcalculator" method="post" onSubmit="return validatecal(this)">



               <label >Select Units :</label>
                <select name="unit2" id="unit2"  onChange="checkunit()" >      
                <option value="CK">CM/KG</option>
                <option value="IP">Inches/Pounds</option>
           </select>
             </div> 
             </div>



             <label >Enter Dimensions :</label>
             <label ">Length</label>
             <input type="text" name="vm_length"  id="vm_length"  > 
             <span id="unittype1" >cm</span>




             <label ">Width</label>
             <input type="text" name="vm_width" id="vm_width"  /> 
             <span id="unittype2" >cm</span>



             <label ">Height</label>
             <input type="text" name="vm_height" id="vm_height"  />  
             <span id="unittype3" >cm</span>


              <label >Dimensional Weight :</label>
              <input type="text"disabled="disabled"name="total_weight" id="total_weight"  >
              <input type="submit" value="Calculate"  name="Submit"/>&nbsp;
              <input type="reset" value="Clear" name="Submit2" />

            </form> 
       </body>
</html> 

I want the code to get the output, trying to calculate the Dimensional Weight (L x W x H / 5000), please can some one help me?

After entering the length, width, height values i am unable to get the dimensional weight.

Upvotes: 0

Views: 59

Answers (1)

Jitendra Khatri
Jitendra Khatri

Reputation: 774

You are using IsNumber() which is not a native function. Try this code

<html>
	<head>
		<script type="text/javascript">
		function checkunit()
		{
			if(document.getElementById("unit2").value=="CK")
			{
				document.getElementById("unittype1").innerHTML='cm';
				document.getElementById("unittype2").innerHTML='cm';
				document.getElementById("unittype3").innerHTML='cm';
			}
			else
			{
				document.getElementById("unittype1").innerHTML='in';
				document.getElementById("unittype2").innerHTML='in';
				document.getElementById("unittype3").innerHTML='in';
			}
		}

		function validatecal(form)
		{
			debugger;
			if (form.vm_length.value=="" 
				|| form.vm_length.value==0) {
				alert('Enter Length');
				form.vm_length.focus();

				return false
			}
			
			if (isNaN(form.vm_length.value)
				&& form.vm_length.value!="") {
				alert('Invalid Length');
				form.vm_length.focus();
			
				return false
			}
			
			if (form.vm_width.value=="" 
				|| form.vm_width.value==0) {
				alert('Enter Width');
				form.vm_width.focus();

				return false
			}

			if (isNaN(form.vm_width.value) 
				&& form.vm_width.value!="") {
				alert('Invalid Width');
				form.vm_width.focus();

				return false
			}
			
			if (form.vm_height.value=="" 
				|| form.vm_height.value==0) {
				alert('Enter Height');
				form.vm_height.focus();

				return false;
			}
			if (isNaN(form.vm_height.value)
				&& form.vm_height.value!="") {
				alert('Invalid Height');
				form.vm_height.focus();

				return false
			}
			calcVolWeight();
			return false;
		}

		function calcVolWeight() {
			var lengthValue = document.getElementById("vm_length").value;
			var widthValue = document.getElementById("vm_width").value;
			var heightValue = document.getElementById("vm_height").value;
			var weightDivisor = document.getElementById("unit2").value == "IP" ? 139 : 5000;
			var roundFactor = document.getElementById("unit2").value == "IP" ? 1 : 0.5;
			var originalVolume = lengthValue * widthValue * heightValue;
			// Calculate the volumetric weight
			var calcVolWeight = roundFactor * Math.ceil(( originalVolume / weightDivisor ) / roundFactor);
			document.getElementById("total_weight").value = parseFloat(calcVolWeight);
		}
		</script>
	</head>
	<body>
		<form name="vcalculator" id="vcalculator" method="post" onSubmit="return validatecal(this)">
			<label >Select Units :</label>
			<select name="unit2" id="unit2"  onChange="checkunit()" >
				<option value="CK">CM/KG</option>
				<option value="IP">Inches/Pounds</option>
			</select>
		</div>
	</div>
	<label >Enter Dimensions :</label>
	<label>Length</label>
	<input type="text" name="vm_length"  id="vm_length"  >
	<span id="unittype1" >cm</span>
	<label ">Width</label>
	<input type="text" name="vm_width" id="vm_width"  />
	<span id="unittype2" >cm</span>
	<label ">Height</label>
	<input type="text" name="vm_height" id="vm_height"  />
	<span id="unittype3" >cm</span>
	<label >Dimensional Weight :</label>
	<input type="text"disabled="disabled"name="total_weight" id="total_weight"  >
	<input type="submit" value="Calculate"  name="Submit"/>&nbsp;
	<input type="reset" value="Clear" name="Submit2" />
</form>
</body>
</html>

Upvotes: 1

Related Questions