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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing Money Values???

Status
Not open for further replies.

scripter73

Programmer
Apr 18, 2001
421
US
Hi,

** PLEASE DON'T GET INTIMIDATED BY THIS POST'S LENGTH. I THINK ITS AN EASY ONE (THOUGH I CAN'T CRACK IT.) :) **

I have a form that has 3 radio buttons. The 1st radio button is a minimum value, the 2nd is a maximum value, and the 3rd one has a text box associated with it where the user can enter dollar amounts. (Keep in mind this is a text box.) The values for these radio buttons are held in hidden fields on the form.

When the user clicks Submit, a Javascript validation function is invoked. It checks the following:
1) If the user picked the 3rd radio button, then verify they entered an amount (this works).

2) Compare the 3rd box value with the other two box values.

Condition: (if 3rd box value is < 1st box value) alert user
OR (if the 3rd box value is > 2nd box value) alert user (here's my problem - this isn't working).

Here's my Javascript code:


if (document.payment_form.cc_payment[2].checked)
{
//require text box be filled in
if (document.payment_form.otheramt.value == &quot;&quot; || document.payment_form.otheramt.value == null){
alert(&quot;Please enter a dollar amount for payment.&quot;);
return false;
}


//validate text box to make sure it is the right amount
alert(&quot;Min amt is &quot; + document.payment_form.min_amt.value);
alert(&quot;Max amt is &quot; + document.payment_form.max_amt.value);
alert(&quot;Other amt is &quot; + document.payment_form.otheramt.value);

var min_curr = document.payment_form.min_amt.value;(1st radio button value)
var max_curr = document.payment_form.max_amt.value;(2nd radio button value)
var oth_curr = document.payment_form.otheramt.value;(3rd radio button value)

if (oth_curr < min_curr){
alert(&quot;Other currency entered is less than the minimum amt due! Please enter another amount.&quot;);
return false;
}

if (oth_curr > max_curr){
alert(&quot;Other currency entered exceeds the maximum amount due! Please enter another amount.&quot;);
return false;
}





The problem is even if I force a condition that should trigger the second alert message, I still get the first. That tells me Javascript's not recognizing these values as I've intended.

I'm thinking its because I'm referencing the hidden &quot;text&quot; boxes and these are not considered numeric or floating values. I tried using the parseFloat(), but that doesn't work. I get the same thing.

Any ideas??? Any help is greatly appreciated.

Thanks,
scripter73




Change Your Thinking, Change Your Life.
 
Try forcing a conversion to numerical values, since input values are typically processed as strings:

var min_curr = document.payment_form.min_amt.value * 1;
var max_curr = document.payment_form.max_amt.value * 1;
var oth_curr = document.payment_form.otheramt.value * 1;

Also, just to avoid errors, you might use:

if (document.payment_form.otheramt.value.length == 0 || isNaN(document.payment_form.otheramt.value * 1))
{
alert(&quot;Please enter a dollar amount for payment.&quot;);
return false;
}
 
Thanks for your response, trollacious.

I finally! got the variables to be recognized as numeric with the following:

var min_curr = parseInt(document.payment_form.min_amt.value);
var max_curr = parseInt(document.payment_form.max_amt.value);
var oth_curr = parseInt(document.payment_form.otheramt.value);


I had a function to tell me if they were considered numeric or not. And they are.

However, now I'm just trying to do a simple comparison and its like Javascript can't read the values. All I'm saying is the following:


if (oth_curr < min_curr){
alert(&quot;Other currency entered is less than the minimum amt due! Please enter another amount.&quot;);

}

if (oth_curr > max_curr){
alert(&quot;Other currency entered exceeds the maximum amount due! Please enter another amount.&quot;);

}



The amounts are as follows:

min_curr = 505.50
max_curr = 1651.51
oth_curr = 15,000.00

Is it something I'm doing wrong. This seems to simple to mess up.

Thanks,
scripter73
Change Your Thinking, Change Your Life.
 
Did you ever find a solution for this. I am experiencing something similar.

Here is my code
Code:
<script language="JavaScript1.2">
<!--   # Below is the Maximum Order Quantity Logic # //-->
function doMaxOrdQ() {
var entered_qty = document.qtyForm.f_ItemQty.value
var maxordqty = document.qtyForm.f_MaxOrdQty.value
if (maxordqty>0)
	{
		if (entered_qty>maxordqty) 
		{
			document.qtyForm.f_ItemQty.select()
			document.qtyForm.f_ItemQty.focus()
			return false
			}
		else
		{
			return true
		}
	}
	else
	{
	//	Max qty is zero so there is no ceiling limit
	return true
	}	
}
</script>

I believe it does not treat the values as numeric.

Can someone please help.

Thanks in advance.
 
try:

var entered_qty = parseint(document.qtyForm.f_ItemQty.value)
var maxordqty = parseint(document.qtyForm.f_MaxOrdQty.value)

the parseint() converts the value into numeric.


____________________________________
Just Imagine.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top