0

I am having some javascript coding/comparing issues. alert(myfn) gives me 78>=99.35. How can I use this in function now?

if (document.getElementById('<%=g.ClientID%>').innerHTML != '') {
    var myvar = '';
    var myfn = '';
    myvar = document.getElementById('<%=a.ClientID%>').value + document.getElementById('<%=g.ClientID%>').innerHTML;
    //alert(myvar);
    myfn = myvar.replace('&gt;', '>').replace('&lt;', '<');
    alert(myfn);
    if (myfn) {
        alert("t");
    }
    if (78>=99.35) {
        alert("m");
    }
    //alert(Boolean(myfn));
}
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Can you be more specific , please – T.S. May 27 '15 at 19:55
  • `eval('78>=99.35')` gives you `false`, however it costs. – Leo May 30 '15 at 11:40
  • possible duplicate of [Safe evaluation of arithmetic expressions in Javascript](http://stackoverflow.com/questions/5066824/safe-evaluation-of-arithmetic-expressions-in-javascript) –  May 30 '15 at 14:11
  • You should use one the many libraries available to evaluate expressions. See the question suggested as a duplicate for ideas. –  May 30 '15 at 14:12

1 Answers1

-1

You can use eval function.

http://www.w3schools.com/jsref/jsref_eval.asp

The eval() function evaluates or executes an argument.

If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.

alert(eval("78>=99.35"));

UPDATE:

If you need to do only arithmetic evaluation, then you can use following code to make eval safer (as @JosephMalle point it out)

var exp = "78>=99.35";

exp = exp.replace(/[^0-9<>=\-()\.]/g, "");
// or 
exp = exp.replace(/[^0-9<>=\-()\.+*/|&\?%:]/g, "");
// which also includes bitwise, logical 
// and conditional (ternary) operators
alert(eval(exp));
Ersin Basaran
  • 517
  • 3
  • 8