2 Replies Latest reply on May 14, 2015 4:10 AM by BKBK

    What is wrong with using isValid() and this regex?

    PhilBro

      I'm validating US and Canadian postal codes using isValid(). US validates as expected no matter what I pass to it. It is the Canadian postal code that isn't validating the way I expect.

       

      The regex I'm using

      ^[0-9]{5}(-[0-9]{4})?|^(?i)(([ABCEGHJ-NPRSTVXY][0-9][ABCEGHJ-NPRSTV-Z])( ?)([0-9])([ABCEGHJ-NPRSTV-Z]{1}[0-9]))$

       

      Postal code

      L4K 2M9

       

      IsValid() will return true with that postal code. Add 1 or more spaces to the end of the code and it still returns valid. ReFind with the same regex works as expected. Am I missing something here?

       

      Edit:

      I just found that using param with type='regex' does the same thing.

      param name='arguments.postalCode' type='regex' pattern='^[0-9]{5}(-[0-9]{4})?|^(?i)(([ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}) [ ]?([0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}))$' default='12345' ;

       

      Another Edit:

      Looks like leading and trailing spaces are ignored when using regex with isValid or a param.

        • 1. Re: What is wrong with using isValid() and this regex?
          PhilBro Level 1

          It looks like using IsValid() is never going to be a good idea. I've also determined that cfparam and param also have issues when using type='regex'.

           

          I don't know if this is the best solution but I've switched to using ReFind() to validate using regex. Is there a better way of validating vars and passed arguments?

          • 2. Re: What is wrong with using isValid() and this regex?
            BKBK Adobe Community Professional & MVP

            Edit:

            I just found that using param with type='regex' does the same thing.

            param name='arguments.postalCode' type='regex' pattern='^[0-9]{5}(-[0-9]{4})?|^(?i)(([ABCEGHJ-NPRSTVXY]{1}[0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}) [ ]?([0-9]{1}[ABCEGHJ-NPRSTV-Z]{1}[0-9]{1}))$' default='12345' ;

             

            Another Edit:

            Looks like leading and trailing spaces are ignored when using regex with isValid or a param.

            That's right. The function implicitly does a trim before validating.