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 Coder
    Join Date
    Jan 2012
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Simple calc with eval

    Code:
    <HTML>
    
    <HEAD>
    
    <TITLE>John's JavaScript!</TITLE>
    
    <SCRIPT TYPE="text/javascript">
    
    function solve(math)
    {
    form.answer.value = eval(math);
    }
    
    </SCRIPT>	
    
    </HEAD>
    
    <BODY>
    
    <FORM METHOD="POST">
    Enter a JavaScript mathematical expression:
    <INPUT TYPE="text" NAME="entry" VALUE="" />
    	<BR />
    The result of this expression is:
    <INPUT TYPE="text" NAME="answer" VALUE=""
    	onFocus="blur()"/>
    <INPUT TYPE="button" NAME="submit"
            onClick="solve(form.entry.value)"/>
    
    </BODY>
    
    </HTML>
    The only problem I am facing is changing form.answer.value
    It won't change, why not?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,603
    Thanks
    80
    Thanked 4,500 Times in 4,464 Posts
    Because you are using form. without ever giving a value to the variable form

    A better way to do this:
    Code:
    <form onsubmit="return false;"><!-- ensure this form can't actually be submitted -->
    Enter a JavaScript mathematical expression: <input name="entry" />
    <br/>
    The result of this expression is: <input readonly="readonly" name="answer" />
    <br/>
    <input type="button" name="submit" onclick="this.form.answer.value=eval(this.form.entry);" />
    </form>
    By the by:
    (1) UPPER CASE tag and property names are obsolescent in modern xhtml, which almost everybody now users.
    (2) If you don't have a </form> tag, then you can never refer to form elements via form.

    If you insist on moving the JS code to the <head>, then you will need to "find" the <form>, so:
    Code:
    function solve( math )
    {
        var form = document.forms[0]; 
        form.answer.value = eval( math );
    }
    
    and
    
    <input type="button" name="submit" onclick="solve(this.form.entry.value);" />
    or
    <input type="button" name="submit" onclick="solve(document.forms[0].entry.value);" />


    [/code]
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    One thing to note is that your code will not just EVALuate calculations. It will also run scripts that will be able to do anything they like to the web page.

    You should NEVER use eval in JavaScript in web pages that you expect anyone other than you to have access to.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Dec 2011
    Location
    Earth if you want to know
    Posts
    27
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by felgall View Post
    One thing to note is that your code will not just EVALuate calculations. It will also run scripts that will be able to do anything they like to the web page.

    You should NEVER use eval in JavaScript in web pages that you expect anyone other than you to have access to.
    Hi,

    The only way I know to calculate a string is to use eval() OR looping through each of the units in the string and use isNaN function to calculate it. Using eval() sounds more simple and realistic. However, you should make sure that the input is a valid operation by running Reg Test:

    PHP Code:
    var $string "1+2+2*4";
    $string.replace(/[^-()d/*+.]/g, '');
    $result = eval($string); 
    I hope this help.

    Tim._

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    That expression should prevent most attacks and will also block any calculations that require the use of the Math object.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

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