Link to home
Start Free TrialLog in
Avatar of srividya_kris
srividya_kris

asked on

validate and evaluate arithmetic expressions

Can any one help me with a good code in javascript to validate and evaluate the arithmetic expression entered in a text box item.
Avatar of xabi
xabi

Try this:

<html>
<head>
<script>
<!--
function errorHandler(message,url,line) {
  alert("Invalid expression")
  return true
}

function doit(idform) {
 eval ("idform.result.value = " + idform.expression.value)
}
window.onerror = errorHandler
//-->
</script>
</head>
<body>
<form name="myform">
Expression: <input type="text" name="expression"><br>
Result: <input type="text" name="result" readonly><br><br>
<input type="button" value="Evaluate" onclick="doit(this.form)">
</form>
</body>
</html>

xabi
ASKER CERTIFIED SOLUTION
Avatar of sureshkumar
sureshkumar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sureshkumar:

It's nice to see a how someone cut and paste your code, change a little things and post it as a answer.

Question:

Where your code validate the expression?

xabi