Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

New Here ,
May 05, 2015 May 05, 2015

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.

249
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 07, 2015 May 07, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 14, 2015 May 14, 2015
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources