Solved

validate and evaluate arithmetic expressions

Posted on 2000-02-23
3
862 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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

687 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