Highlighted

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

New Here ,
May 05, 2015

Copy link to clipboard

Copied

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.

Views

152

Likes

Translate

Translate

Report

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

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

New Here ,
May 05, 2015

Copy link to clipboard

Copied

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.

Views

153

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 05, 2015 0
New Here ,
May 07, 2015

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 07, 2015 0
BKBK LATEST
Adobe Community Professional ,
May 14, 2015

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 14, 2015 0