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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Basic displaying of variables in alert

    Hi fellas,

    Been a long time since ive really touched Javascript, and i've forgotten much of the syntax. That said, I can have a complex piece of code in front of me and explain how it works, but cannot write for peanuts.

    I'm basically helping a friend, who had two text fields, which should take numeric values only. This will be the only error checking required. All we need to do is, when the form is processed, multiply the two values entered in each text field, and display the result in an alert box. I also need to get this figure, add 25% to it, and have it multiplied by a figure entered in a third text box, which will also display in the alert box, in the same sentence.

    Anyone able to give me some pointers?

    Cheers.

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Something like this mate?

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script type="text/javascript">
    /* liorean's toDecimals script (http://www.codingforums.com/member.php?u=5798) */
    Number.prototype.toDecimals = function(n) {
        n = (isNaN(n)) ? 2 : n;
        var nT = Math.pow(10,n);
        function pad(s){
                s = s || '.';
                return (s.length>n) ? s : pad(s+'0');
        }
        return (isNaN(this)) ? this : (new String(Math.round(this*nT)/nT)).replace(/(\.\d*)?$/,pad);
    }
    
    function validate(f) {
    	var num1 = parseFloat(f.txtNumber1.value);
    	var num2 = parseFloat(f.txtNumber2.value);
    	var num3 = parseFloat(f.txtNumber3.value);
    	
    	if(isNaN(num1)) {
    		alert("Number 1 must be a number");
    		return;
    	} else if(isNaN(num2)) {
    		alert("Number 2 must be a number");
    		return;
    	} else if(isNaN(num3)) {
    		alert("Number 3 must be a number");
    		return;
    	}
    	
    	var result1 = (num1*num2).toDecimals();
    	var result2 = ((parseFloat(result1)+(parseFloat(result1)/4))*num3).toDecimals();
    	
    	alert("Number 1 * Number 2 = "+result2+"\nNumber 1 * Number 2 + 25% * Number 3 = "+result2);
    }
    </script>
    </head>
    
    <body>
    <form name="frm" onsubmit="validate(this); return false;">
    <input type="text" name="txtNumber1">&nbsp;Number 1<br>
    <input type="text" name="txtNumber2">&nbsp;Number 2<br>
    <input type="text" name="txtNumber3">&nbsp;Number 3<br><br>
    <input type="submit" value="submit">
    </form>
    </body>
    </html>
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #3
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cheers mate! Thanks a lot for taking the time to do that.

    Only this is, it looks a little complicated for the outcome, the code above the main function is *largely* greek to me, and i'll definetly have a hard time getting my bud to understand it! Though I gather I can prune it somehow.

  • #4
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    the bit above the Number.prototype.toDecimals is an extension to the standard javascript Number object. The toDecimals method converts the number to the specified number (or two if not specified) of decimal places (with appropriate zeros).

    For instance:
    Code:
    var myNumber = 10.123456;
    var myRoundedNumber = myNumber.toDecimals(); // returns 10.12
    
    var myNumber = 10.1;
    var myRoundedNumber = myNumber.toDecimals(3); // returns 10.100
    Here is a simplified version of the function:
    Code:
    /* extend the JavaScript number object with the new method toDecimals */
    Number.prototype.toDecimals = function(d) {
    	/* if no decimal places supplied, round to 2 decimal places */
    	d = d || 2; 
    
    	var step1 = this*Math.pow(10, d); // multiply number (referred to as this) by 10 to the power of the number of decimal places required
    	var step2 = Math.round(step1); // round that result
    	var step3 = step2 / Math.pow(10, d) // divide the rounded number by 10 to the power of the number of decimal places required
    
    	return step3; // return the rounded number
    }
    There's plenty of tutorials on the web explaining the maths behind rounding numbers in greater detail, I just hope i've given you some initial insight into it.

    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper

  • #5
    New to the CF scene
    Join Date
    Aug 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Champ! Thanks for your time.


  •  

    Posting Permissions

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