Solved

validate and evaluate arithmetic expressions

Posted on 2000-02-23
3
859 Views
Last Modified: 2008-03-04
Can any one help me with a good code in javascript to validate and evaluate the arithmetic expression entered in a text box item.
0
Comment
Question by:srividya_kris
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Expert Comment

by:xabi
ID: 2550024
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
0
 
LVL 1

Accepted Solution

by:
sureshkumar earned 100 total points
ID: 2553220
<html>
<head>
<script language="javascript">
function evaluate(form)
{
if(form.elements[0].value.length==0){
alert("sorry you have to enter expression");
}
form.elements[1].value=eval(form.elements[0].value);
}
</script>
</head>
<body>
<form>
<input type=text name=expre><br>
<input type=text name=value><br>
<input type=button value="click" onClick="evaluate(this.form)">
</form>
</body>
</html>

further queries
suresh kumar
0
 
LVL 8

Expert Comment

by:xabi
ID: 2553479
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question