Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    308
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Automatically removing a comma when tabbing off a field

    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. :(

    Code:
    <!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>
    Last edited by AshleyQuick; 05-30-2004 at 05:12 PM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •