Solved

validate and evaluate arithmetic expressions

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

757 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

22 Experts available now in Live!

Get 1:1 Help Now