Skip to main content
Known Participant
May 5, 2015
Question

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

  • May 5, 2015
  • 1 reply
  • 278 views

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.

    This topic has been closed for replies.

    1 reply

    PhilBroAuthor
    Known Participant
    May 7, 2015

    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?

    BKBK
    Community Expert
    Community Expert
    May 14, 2015

    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.