Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Box 1 greater than box 2 alert works only sometimes?! 1

Status
Not open for further replies.

Katie6

Programmer
Jun 12, 2007
58
GB
Hi,

I have a some javascript which checks that the number in the left text box is less than or equal to the number in the right box. It sometimes works, but not always. For example, it will correctly bring up an error message if I put 5 out of 4 in the boxes or 56 out of 55. But if the boxes contain a different number of digits the error message does not show. For example, 100 out of 55 does not work and 10 out of 5 does not work. It is as if the third digit is being chopped off and the code sees the numbers as 10 out of 55 and 1 out of 5.

Here is the code:

Code:
<html>
<head>
<script type="text/javascript" language="JavaScript">

function CheckValue(obj)
{
var value1 = document.getElementById('q'+obj+'a'+'_element');
var value2 = document.getElementById('q'+obj+'b'+'_element');

    if(value1.value !='' && value2.value !='')
    {
        if(value1.value > value2.value)
	{
            alert('The value in the right field must be greater than or equal to the value in the left.  Please correct your answer.');
        }
    }
}
</script>
</head>
<body>

How many samples?<br />

<input id="q1a_element" type="text" name="q5a" value="" onchange='CheckValue(1);'> correct samples out of
<input id="q1b_element" type="text" name="q5b" value="" onchange='CheckValue(1);'> samples reviewed

<br />
<ul></ul></label> 


</body>


</html>

I was wondering if someone might be able to explain what I've done wrong and how to fix it. I'm baffled!

Many thanks,

Katie
 
The data needs to be turned into a number (currently a string when you get it from the value attribute):
Code:
value1 = parseFloat(value1.value);
value2 = parseFloat(value2.value);

See how that goes.

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Thanks Jeff! That works a treat. For reference, this is the working code:

Code:
<html>
<head>
<script type="text/javascript" language="JavaScript">

function CheckValue(obj)
{
var value1 = document.getElementById('q'+obj+'a'+'_element');
var value2 = document.getElementById('q'+obj+'b'+'_element');

    if(value1.value !='' && value2.value !='')
    {	

        if(parseFloat(value1.value) > parseFloat(value2.value))
	{
            alert('The value in the right field must be greater than or equal to the value in the left.  Please correct your answer.');
        }
    }
}
</script>
</head>
<body>

How many samples?<br />

<input id="q1a_element" type="text" name="q5a" value="" onchange='CheckValue(1);'> correct samples out of
<input id="q1b_element" type="text" name="q5b" value="" onchange='CheckValue(1);'> samples reviewed

<br />
<ul></ul></label> 


</body>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top