...

View Full Version : Automatically removing a comma when tabbing off a field



AshleyQuick
05-30-2004, 06:10 PM
I have an payment calculator that does not allow commas. Seems I have two choices:

1. Implement a script that will simply remove commas before submitting the form

2. Change the script to allow for commas

Unfortunately, I've reached an impasse with each. :(


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script>
<!--//
function checkNumberEntry(input, min, max, msg) {

var str = input.value;

if (str == null || input.length == 0) {
msg = msg + " is blank. You must fill in this field to calculate.";
alert(msg);
return false;
}

for (var i = 0; i < str.length; i++) {
var curr_ch = str.substring(i, i + 1)
if ((curr_ch < '0' || curr_ch > '9') && curr_ch != '.') {
msg = msg + " must only contain digits. You entered: " + input.value;
alert(msg);
return false;
}
}
var num = input.value;
if ((min != -9999) && num<min) {
msg = msg + " that's too low: " + input.value + ". You should only enter values greater than " + min + ".";
alert("You have entered a " + msg);
return false;
}

if ((max != -9999) && num > max) {
msg = msg + " that's high: " + input.value + ". You should only enter values less than " + max + ".";
alert("You have entered a " + msg);
return false;
}
input.value = str;
return true;
}
function myRound(val) {

val = val * 100;
val = Math.round(val);
return val / 100;
}

function computeField(input) {

if (input.value != null && input.value.length != 0)
input.value = "" + eval(input.value);
compute(input.form);
}
function compute(form) {
var price = form.price.value;
var interest = form.interest.value;
var months = form.months.value;

if (!checkNumberEntry(form.price, 0, -9999, "Price")) {
form.payment.value = "Invalid";
return;
}
if (!checkNumberEntry(form.down, 0, parseFloat(price), "Down payment")) {
form.payment.value = "Invalid";
return;
}
if (!checkNumberEntry(form.months, 1, -9999, "# of months")) {
form.payment.value = "Invalid";
return;
}
if (!checkNumberEntry(form.interest, 1, 99, "% interest Rate")) {
form.payment.value = "Invalid";
return;
}

price = price - form.down.value;
interest = (interest / 100.0) / 12;

var pow = 1;
for (var j = 0; j < months; j++)
pow = pow * (1 + interest);

var pmt = (price * pow * interest) / (pow - 1);

form.payment.value = myRound(pmt);

}
function clearForm(form) {
form.payment.value = "";
}
//-->
</script>
</head>
<body>
<table>
<form>
<tr>
<td>Car Price</td>
<td><input type="text" name="price" size="9"></td>
</tr>
<tr>
<td>Loan Term (in months)</td>
<td><input type="text" name="months" size="9"></td>
</tr>
<tr>
<td>Interest Rate (%)</td>
<td><input type="text" name="interest" size="9"></td>
</tr>
<tr>
<td>Down Payment</td>
<td><input type="text" name="down" size="9"></td>
</tr>
<tr>
<td>Your Monthly Payment</td>
<td><input type="text" name="payment" size="9"></td>
</tr>
<tr>
<td><input type="button" value="compute" onclick="compute(this.form)"></td>
<td><input type="reset" value="clear fields" onclick="clearform(this.form)"></td>
</tr>
</form>
</table>
</body>
</html>

glenngv
05-31-2004, 06:56 AM
http://www.codingforums.com/showthread.php?t=25870#post132401



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum