i have a text box in which i give an arithmetic expression. Something like (a-b)/(a/5)*(B+2) some similar expressions.i need to get the validation for the given expression. only one result out of this.result is secondary.Primary requirement is the validation. if the expression is not correct i should get it as "Invalid Expression".
2 Answers
If you work on windows forms environment you've got an event called Leave, when you enter a textbox and leave it this event will automatically raise, so you can write an event handler to check validation of the textbox input, if you work in asp.net and web forms application you can use JavaScript or jQuery to validate the result or you can use asp.net built in validation controls..

- 56
- 6
-
yeah.but i should validate the expression given..how can i give that?? 'm actually asking about validating the expression not about displaying the validation "message".' m working on web forms. i have tried something like this.. function mathEval (exp) { var reg = /^(?=.*\d{2})(?=.*[a-zA-Z]{2}).{8,}$/; if (!reg.test(exp)) { alert('no'); return false; } else { alert('yes'); return true; } – Prasii Jul 09 '13 at 06:46
-
you can do this by regular expression validator in asp.net, if you choose client side validation in this tool it will build the javascript code for you – MAK Jul 09 '13 at 09:18
-
A regular expression will not do because a finite state machine is not powerful enough to handle simple cases such as balancing parentheses. You need at least a stack machine to do that. – Tarik Jul 09 '13 at 15:32
-
Interesting solutions can be found here: http://stackoverflow.com/questions/355062/is-there-a-string-math-evaluator-in-net/392355#392355 – Tarik Jul 09 '13 at 15:35
You're looking for an evaluation evaluator which involves parsing the text. It appears you want this in javascript (you haven't been specific enough) but I think the accepted answer on this SO: Safe evaluation of arithmetic expressions in Javascript will probably help you.
For the record, also, I don't believe that this kind of answer deserves any +1s because all I did was google this for you.

- 1
- 1

- 41,961
- 13
- 104
- 160
-
thanks.the link is working fine for numeric values.my requirement is: in one page i use variables (A+B)*(C-D)/E like this. i donn give any numerics here. for these variables i give values in another page. for A =1, B=2 ,c= 12 etc... what should be done is, it should check the expression (A+B)*(C-D)/E and tell whether it is valid or not. if i give something like (A+B-C+-( it should give as invalid. and ofcourse am using javascript. please be patient with me. am new to c# as well as java script... – Prasii Jul 09 '13 at 08:19
-
-
i am not asking for evaluating the result..that will be done in some other page.. first thing we do is give an arithmetic expression with variables.. after that the values for these variables will be given in some other page and the result will be evaluated then.. evaluating the result is secondary and i donn need it here. what i need is i need to give an expression with variables Eg: (a+b)/(b*d-a)+a This should be checked and validation message should be thrown if we give it in incorrect format. Eg: (a+b++)(c-*d)(( – Prasii Jul 09 '13 at 10:15
-
no I know you're not but that evaluator might have a straightforward 'parse' method and, if it doesn't, it'll almost certainly throw an exception if it fails to parse. You can simply wrap that in a `try..catch` and display an error message that way. – Andras Zoltan Jul 09 '13 at 22:58
-
I have been trying hard for the regular expression pattern for this arithmetic expression.But couldnt make it out.Can u please give me a pattern to accept only one operator between the operands. so that it would atleast make my expression minimum sensible... – Prasii Jul 12 '13 at 06:48