Solved

validate and evaluate arithmetic expressions

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now