Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Formula Validation

Question posted by: pankajit09 (Familiar Sight) on April 25th, 2008 01:41 PM
Hello,

I have a expression like this -->

Code: ( text )
  1. (a+b/c)


How to validate it ?

For eg: if the expression is (a+b/c then error should be thrown.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
markusn00b's Avatar
markusn00b
Expert
1,886 Posts
April 25th, 2008
05:10 PM
#2

Re: Formula Validation
Quote:
Originally Posted by pankajit09
Hello,

I have a expression like this -->

Code: ( text )
  1. (a+b/c)


How to validate it ?

For eg: if the expression is (a+b/c then error should be thrown.


I dont understand.
How would the user give you the formula?

Reply
pankajit09's Avatar
pankajit09
Familiar Sight
249 Posts
April 27th, 2008
08:48 AM
#3

Re: Formula Validation
Quote:
Originally Posted by markusn00b
I dont understand.
How would the user give you the formula?



The user will enter the formula in a textbox which is present in a form.

Reply
aktar's Avatar
aktar
Member
90 Posts
April 27th, 2008
11:50 AM
#4

Re: Formula Validation
Try this:

in your code will be :
Code: ( text )
  1. $a = [the value];
  2. $b = [the value];
  3. $c= [the value];
  4.  
  5. $correct_answer = [the correct answer];
now you can convert the formula text you receive from the user, like so:
Code: ( text )
  1. ($a+$b)/$c

then you can use a fuction like eval() to calculate a value based on the users input. Then its just a matter of matching it with the $correct_answer

like so:
Code: ( text )
  1. eval('$user_answer = ($a+$b)/$c;');
  2.  
  3. if ($user_answer != $correct_answer){
  4.  
  5.   //TRHOW ERROR!!!
  6.  
  7. }
Regards

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
April 27th, 2008
10:18 PM
#5

Re: Formula Validation
If users are writing their own functions then you will need a lot of searching and counting in the strings, so before I spend time on that tell me if it's a set formula or not. If it is aktar's code looks good!

Reply
pankajit09's Avatar
pankajit09
Familiar Sight
249 Posts
May 15th, 2008
06:25 AM
#6

Re: Formula Validation
Quote:
Originally Posted by TheServant
If users are writing their own functions then you will need a lot of searching and counting in the strings, so before I spend time on that tell me if it's a set formula or not. If it is aktar's code looks good!



The formula will be limited to BODMAS like (x+y)/z

I just want to validate it .

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 15th, 2008
06:58 AM
#7

Re: Formula Validation
Quote:
Originally Posted by pankajit09
The formula will be limited to BODMAS like (x+y)/z
I just want to validate it .



So a user could enter something like x+(y*z)/2+y*x???

If so, this is complicated, and I am about to go home. Please confirm this is the case, because if users can enter their own formulae it means that you need to make lots and lots of rules to test each string by, and no point in wasting time on it if I have misunderstood.

Reply
pankajit09's Avatar
pankajit09
Familiar Sight
249 Posts
May 16th, 2008
07:10 AM
#8

Re: Formula Validation
Quote:
Originally Posted by TheServant
So a user could enter something like x+(y*z)/2+y*x???

If so, this is complicated, and I am about to go home. Please confirm this is the case, because if users can enter their own formulae it means that you need to make lots and lots of rules to test each string by, and no point in wasting time on it if I have misunderstood.



yes you are right.

Has someone did it ?

Reply
dlite922's Avatar
dlite922
Needs Regular Fix
491 Posts
May 16th, 2008
07:52 AM
#9

Re: Formula Validation
Quote:
Originally Posted by pankajit09
yes you are right.

Has someone did it ?


BODMAS excercises?

http://www.abacustraining.biz/bodmasExercises.htm

What is an example of what the user exactly entering...

This:

Code: ( text )
  1. 3*2(2+4/2)-13


OR

This:

Code: ( text )
  1. x+y(z-z*2)/k


are you verifing the "syntax", IOW making sure the parenthesis are matched up and return a true or false,

or do you need to calculate the expression and give the a number result? or perhaps compare it with your number to see if correct?

If you're looking for the first example, then all you need to do is search for parenthesis. count all the left parenthesis and count all the right parenthesis, if the numbers don't match, they left a parenthesis out and their syntax is incorrect.

if second example, then use eval();
if using eval() BE VERY CAREFUL to filter the content, otherwise i can execute any PHP code on your Site!!

for example, use regex to check for characters other than -,+,*,/ 0-9 and a-z;

no semi-colon in content given to eval() should make it a litte safer. maybe user str_replace() and replace all semi-colons with an empty string.

Good luck,


Dan

Reply
Reply
Not the answer you were looking for? Post your question . . .
178,103 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top PHP Forum Contributors