Jump to content

having trouble.....


pikemsu28

Recommended Posts

i'm trying to validate a form that converts pounds to kilos and kilos to pounds. but i want to put a stop that if you are converting pounds to kilos, the user cannot enter more than 550 pounds, and if the user is converting kilos to pounds, they cant enter more than 250 kilos.  i'm using two radio buttons to select if they are entering in pounds or kilos.

 

this is what i have so far:

 

<script type="text/javascript">
function checkWeight()
{
var cweight;
with(window.document.form1)
    {
        cweight = weight;
    }
    
    if(trim(cweight.value) == '')
    {
        alert("Please enter patient's Weight.");
        cweight.focus();
        return false;
    }
    else if(isNaN(trim(cweight.value)))
    { 
alert("Please enter only Numeric Values for Weight.");
cweight.focus();
return false;
    }

    else
    {
        cweight.value = trim(cweight.value);
        return true;
    }
}
</script>
<form id="form1" name="form1" method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
<input id="conv" name="conv" type="radio" value="kg"  /><label for="conv">Kilograms</label>
<input id="conv" name="conv" type="radio" value="lbs" /><label for="conv">Pounds</label>
<label for="weight">Weight of Patient:</label><input id="weight" name="weight" type="text" maxlength="6" class="box" value="<?php if(isset($_POST['weight'])){echo $_POST['weight'];}?>" />  <?php if(!isset($_POST['override'])){ echo $_POST['conv'];}?>
<input id="convert" name="convert" type="submit" class="button" value="Convert Weight" onclick="return checkWeight()" />

 

if conv == 'lbs' then the value needs to be less than 551. if conv == 'kg' the value needs to be less than 251.  i'm not sure on how to go about this.  i've done some look up andi know that the radio buttons need to go thru a for loop to see which one is check, but as for that, i'm not sure how to implement that into my code.  if someone could help me out, i would really appreciate it!

 

thanks,

jason

Link to comment
Share on other sites

i tried it like this but it didnt work.  i have no experience with javascript so i dont know where to go from here.

 

function checkWeight()
{
var cweight, cconv;
with(window.document.form1)
    {
        cweight = weight;
	cconv = conv;
    }
    
    if(trim(cweight.value) == '')
    {
        alert("Please enter patient's Weight.");
        cweight.focus();
        return false;
    }
else if(isNaN(trim(cweight.value)))
{
	alert("Please enter only Numeric Values for Weight.");
	cweight.focus();
	return false;
}
else if(trim(cconv.value)=='kg')
{
	if(trim(cweight.value)>250)
	{
		alert("Please verify Weight.");
		cweight.focus();
		return false;
	}
}
else if(trim(cconv.value)=='lbs')
{
	if(trim(cweight.value)>550)
	{
		alert("Please verify Weight.");
		cweight.focus();
		return false;
	}
}
    else
    {
        cweight.value = trim(cweight.value);
cconv.value = trim(cconv.value);
        return true;
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.