Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to trap a IsNan eval expression?

Status
Not open for further replies.

gdrenfrew

Programmer
Aug 1, 2002
227
GB
Hello,
I need to trap an eval expression, perhaps using IsNan. What I've currently got results in errors via the browser. I can't seem to catch the result of eval via IsNan. Any ideas appreciated.

I'm trying to evaluate the total of an arithmetic formula.
eg. expr == "43*45+*34" (This shouldn't evaluate to an number, should it?)

I try to check if it's valid like this

if (isNan(eval(expr)) {
alert("invalid expression")
}

but depending on what I put in the expression (any combination of numbers and arithmetic operators), I get syntax errors, "; expected" etc.

Any help appreciated.
Graeme
 
how do u get this formula:
"43*45+*34" ?

and try this:
alert(parseInt("43+45+34"))

Known is handfull, Unknown is worldfull
 
Hello, thanks for your reply.

I get the formula via an input box that the user fills in. Because they can put in as many operators as they like (in random order), I need to try and eval it (i think).

graeme
 
Try to think of it as an Excel cell, where the user types in their formula (in this case for a stock count),and the output is the result of the formula.

Graeme
 
well does my function work? even eval will do...

Known is handfull, Unknown is worldfull
 
hi,

no, parseInt("4*5") doesn't evaluate the expression, it just returns "4". So if the expression was "4**5", it would return "4" and I wouldn't know if the incoming expression was invalid or not.

Is the output of eval an object? Can I test to see if an object is created? Or am I on wrong path?
Thanks

 
You can try this page it uses the parseInt() vbkris
is talking about to strip added char's

if NaN is returned it propmts user for input:

<html>
<head>
<title>Options Change</title>
<script language=&quot;JavaScript&quot;>
function chk_val(number,field) {
d = document.test;
if(field.value.length != &quot;&quot;)
{ num = number;
if(!parseInt(num)){
alert(&quot;Numbers Only Please!&quot;);
field.focus();
field.select();
return false;}
else {
num = parseInt(num);
//alert(&quot;Yes a Number..Thanks!&quot;);
return num;}//if parseInt successful send num
}
else { alert(&quot;Enter Number in Field!&quot;);
field.focus();
field.select();
return false;
}
}
function chk_form() {
var d = document.test;
var qty_ea = chk_val(d.vala.value,d.vala);
if(!qty_ea){ return; }
var cs = chk_val(d.valb.value,d.valb);
if(!cs){ return; }
var skid = chk_val(d.valc.value,d.valc);
if(!skid){ return; }
var sum = eval((skid*60)+(cs*10)+ qty_ea);
alert(&quot;your order is &quot;+qty_ea+&quot; each(es).\n&quot;
+cs+&quot; case(s) with &quot;+skid+&quot; skid(s)\n&quot;
+&quot;Your Order Total is &quot; +sum+
&quot; of bags of skittles-Please Brush your teeth.&quot;);
}
</script>
</head>
<body><form name=&quot;test&quot;>
Each:<br>
<input type=&quot;text&quot; name=&quot;vala&quot; size=&quot;20&quot;><br><br>
Case:<br>
<input type=&quot;text&quot; name=&quot;valb&quot; size=&quot;20&quot;><br><br>
Skid:<br>
<input type=&quot;text&quot; name=&quot;valc&quot; size=&quot;20&quot;><br><br>
<input type=&quot;button&quot; value=&quot;Order Total&quot; onClick=&quot;chk_form()&quot;>
</form>
</body>
</html>

2b||!2b
 
pareseInt() doesn't care if it is
a neagative or positive number and
(field.value.length != &quot;&quot;) is my error
so let's do:

<script language=&quot;JavaScript&quot;>
function chk_val(number,field) {
d = document.test;
if(field.value.length > 0) //correction
{ num = parseInt(number);
if(!num || num < 0){ //lets add protection
//some smart user is going to try a negative number
alert(&quot;Numbers Only Please!&quot;);
field.focus();
field.select();
return false;}
else {
return num;}//if parseInt successful send num
}
else { alert(&quot;Enter Number in Field!&quot;);
field.focus();
field.select();
return false;
}
}

2b||!2b
 
I just put a try...catch around my eval which alerts the user if they enter a junk formula.
Thanks
Graeme
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top