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

Testing a variable's value

Status
Not open for further replies.

Ineedjava

Technical User
Apr 14, 2001
21
US
Hi,

In Javascript, I am testing the value entered into a donation amount box on a form.

I want to make sure that the user enters a donation amount in the box greater than 1. So I am using this if statement: "If ( amountbox > 1 ) ..."

The problem is if a user enters "$.05" in the box, then this value tests higher than 1 because of the $ sign, and then this amount passes to the credit card processor's system, where we get charged $.40 for the transaction.

Is there a simple solution to this problem? Perhaps a way to only accept numeric input in a form's field?

Thanks.
 
Here is some code to make sure the user only enters numeric values. The value must be entered as well.

This would be in a function

var don_amt = document.REPORTNAME.Donation.value
if (document.REPORTNAME.Donation.value != '') {
if (isNaN(don_amt)) {
alert("Donation amount must be numeric");
document.REPORTNAME.Donation.focus();
document.REPORTNAME.Donation.select();
return;
}
}
 
I would use something like the following:
Code:
var donation = parseInt(document.formname.field.value);
if(donation > 1)
 {
  // do whatever
 }
else
 {
  alert("donation must be greater than one");
 }
-gerrygerry
Go To
 
there's no guarantee that someone who will be entering an amount of 100 bucks won't use the $ sign in the field.
Here's a sample code I have come up with:

<html>
<head><title>Donation</title>
<script language=&quot;javascript&quot;><!--

var don
var val

function verify() {

val = document.frm.donation.value

if (val.charAt(0) == &quot;$&quot;) {
don = val.substring(1, val.length)
alert(don)
}
else {
don = val
alert(don)
}

if (isNaN(don)) {
alert(&quot;The amount of your donation must be a number&quot;)
}
else {
if (don < 1) {
alert(&quot;Please donate more then one dollar&quot;)
}
else {
alert(&quot;thank you for your donation&quot;)
}
}
}

//-->
</script>
</head>

<body>
<form name=&quot;frm&quot; action=&quot;post&quot;>
<input type=&quot;text&quot; name=&quot;donation&quot; value=&quot;hello&quot;>
<input type=&quot;button&quot; value=&quot;click&quot; onclick=&quot;verify()&quot;>
</form>

</body>
</html>
 
ooops,
I wished there was an option to modify the post.
Those alert(don) are there for testing and I didn't remove them before I pasted my code into the forum.
 
whoops! i was interrupted in the middle of my post - i meant to put in a function that filters non-numeric data. I know what u mean about that modify button! -gerrygerry
Go To
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top