8 Replies Latest reply on Oct 12, 2018 3:57 AM by VimalPS

    How to use validate a field using regular expression in Apex.

    VimalPS

      Hi All,

      I have a field where user should only enter =,<,> and digits from 0 to 9.

       

      eg :- >20 , < 30 , =40 , <>30, >=20 like this..

       

      If any other characters entered, it'll throw error.

       

      Is it possible to achieve this validation through - Item matches Regular expression.

      I tried with giving pattern like this .*[^0-9<>=].*  

      But never works.

       

      Screen Shot 2018-10-08 at 7.57.43 PM.png

       

      Can someone help on this, please..

       

      Thanks

        • 1. Re: How to use validate a field using regular expression in Apex.
          fac586

          VimalPS wrote:

           

          I have a field where user should only enter =,<,> and digits from 0 to 9.

           

          eg :- >20 , < 30 , =40 , <>30, >=20 like this..

           

          If any other characters entered, it'll throw error.

           

          Is it possible to achieve this validation through - Item matches Regular expression.

          I tried with giving pattern like this .*[^0-9<>=].*

          But never works.

          That expression does pretty much the opposite of what you are trying to achieve: match zero or more characters followed by a single character that isn't one of =,<,> or the digits from 0 to 9, followed by zero or more other characters.

           

          You are probably looking for something like:

           

          ^(=|!=|\^=|<>|<|>|<=|>=) *[0-9]+$

           

          That is, the entire string must consist of any one of the comparison operators =, !=, ^=, <>, <, >, <=, or >=, followed by zero or more spaces, followed by one or more Western Arabic digit.

           

          You might find one of the sites for testing and explaining regular expressions useful.

          1 person found this helpful
          • 2. Re: How to use validate a field using regular expression in Apex.
            VimalPS

            Hi fac586,

             

            Thanks a lot, for the quick help. It worked.

             

            Two places i have doubt,

            1) If i don't want user to enter <>, how can i achieve that. Because we have the condition |<|>| added in our regex (which is actually required) but should not allow <> .

            2)What exactly +$ is doing in this expression.Is that doing the check for digits from 0-9 has only entered after the comparison operator.

             

            And in the site you suggested, can we try Oracle regex here?.

             

            Thanks.

            • 3. Re: How to use validate a field using regular expression in Apex.
              fac586

              VimalPS wrote:

               

              Two places i have doubt,

              1) If i don't want user to enter <>, how can i achieve that. Because we have the condition |<|>| added in our regex (which is actually required) but should not allow <> .

              So why include it in your sample data? Remove the <> option from the first regex group:

               

              ^(=|!=|\^=|<|>|<=|>=) *[0-9]+$

               

              Note that != and ^= mean the same thing so you may want to remove them as well.

              2)What exactly +$ is doing in this expression.Is that doing the check for digits from 0-9 has only entered after the comparison operator.

              + is a quantifier that matches the between one and unlimited of occurrences of the preceding token.

               

              $ is an anchor that asserts that the expression must extend to the end of the input (or line in some contexts). ^ is the opposite and anchors the expression to the start of the input. Used in combination like this they ensure that the entire string matches the expression.

              And in the site you suggested, can we try Oracle regex here?

              Yes. I tested both the incorrect expression from the OP and the correct solution there.

              1 person found this helpful
              • 4. Re: How to use validate a field using regular expression in Apex.
                VimalPS

                Hi fac586,

                 

                Thanks for detailing, as you suggested i removed <> from the expression but it's still allowing the <> operator.

                Currently my condition is like this 

                ^(=|<|>|<=|>=)*[0-9]+$

                 

                I think, |<|>|  this part is allowing the user to enter <> condition. But for me, this is a invalid condition and the regex accepting as valid. Actually it shouldn't.

                 

                Thanks

                • 5. Re: How to use validate a field using regular expression in Apex.
                  fac586

                  VimalPS wrote:

                   

                  Thanks for detailing, as you suggested i removed <> from the expression but it's still allowing the <> operator.

                  Currently my condition is like this

                  ^(=|<|>|<=|>=)*[0-9]+$

                   

                  I think, |<|>| this part is allowing the user to enter <> condition. But for me, this is a invalid condition and the regex accepting as valid. Actually it shouldn't.

                  Your expression is missing a space before the "*". This results in the "*" quantifier being applied to the first group rather than the space, allowing tokens from the group to appear more than once.

                  • 6. Re: How to use validate a field using regular expression in Apex.
                    VimalPS

                    Ohh!!! that is causing the issue..hmm

                     

                    OK.Why i removed the space is because after user input the operator no space is allowed.

                    eg: < 20 is considered as wrong input

                          <20 is correct input.

                     

                    So if i remove the space i cannot achieve validating <> as wrong input.Right?

                    Any work around for this scenario.It seems like i'm trying to achieve something a bit tricky.

                     

                    One work around i found was to add one more validation to check if there is any SPACE character entered.That way i can control it.

                    Is there a way to achieve through the regular expression itself?.

                     

                    Appreciate your comments..always.

                     

                     

                     

                    Thanks.!!

                    • 7. Re: How to use validate a field using regular expression in Apex.
                      fac586

                      VimalPS wrote:

                       

                      Ohh!!! that is causing the issue..hmm

                       

                      OK.Why i removed the space is because after user input the operator no space is allowed.

                      So why did one appear in your test data?

                      eg :- >20 , < 30 , =40 , <>30, >=20 like this..

                      We can only respond to the questions that are actually posted.

                      So if i remove the space i cannot achieve validating <> as wrong input.Right?

                      Of course not, it's simply a case of using an expression that actually fits the requirements. " *" matches zero or more spaces, which was seen as being required to enable all of the test data posted to pass the validation. If spaces are not permitted, then remove this token from the expression:

                       

                      ^(=|<|>|<=|>=)[0-9]+$

                       

                      Any work around for this scenario.It seems like i'm trying to achieve something a bit tricky.

                      The only thing that seems to be tricky is providing clear and unambiguous requirements...

                      1 person found this helpful
                      • 8. Re: How to use validate a field using regular expression in Apex.
                        VimalPS

                        ooops!!! My bad..Sorry for confusing.

                        Yes.I understand.

                         

                        Really appreciate the replies.

                         

                        Thank You.