Formula Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pankajit09
    Contributor
    • Dec 2006
    • 296

    Formula Validation

    Hello,

    I have a expression like this -->

    Code:
    (a+b/c)
    How to validate it ?

    For eg: if the expression is (a+b/c then error should be thrown.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by pankajit09
    Hello,

    I have a expression like this -->

    Code:
    (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?

    Comment

    • pankajit09
      Contributor
      • Dec 2006
      • 296

      #3
      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.

      Comment

      • aktar
        New Member
        • Jul 2006
        • 105

        #4
        Try this:

        in your code will be :[PHP]$a = [the value];
        $b = [the value];
        $c= [the value];

        $correct_answer = [the correct answer];[/PHP]now you can convert the formula text you receive from the user, like so:[PHP]($a+$b)/$c[/PHP]
        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:[PHP]eval('$user_ans wer = ($a+$b)/$c;');

        if ($user_answer != $correct_answer ){

        //TRHOW ERROR!!!

        }[/PHP]Regards

        Comment

        • TheServant
          Recognized Expert Top Contributor
          • Feb 2008
          • 1168

          #5
          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!

          Comment

          • pankajit09
            Contributor
            • Dec 2006
            • 296

            #6
            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 .

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              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.

              Comment

              • pankajit09
                Contributor
                • Dec 2006
                • 296

                #8
                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 ?

                Comment

                • dlite922
                  Recognized Expert Top Contributor
                  • Dec 2007
                  • 1586

                  #9
                  Originally posted by pankajit09
                  yes you are right.

                  Has someone did it ?
                  BODMAS excercises?

                  イベントの開催が決まったら、まずレンタルスペースを探しましょう。 広さ・立地・設備の条件を整理して、目的に合った会場を見つけることが成功への第一歩です。


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

                  This:

                  Code:
                  3*2(2+4/2)-13
                  OR

                  This:

                  Code:
                  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

                  Comment

                  Working...