cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
shanemeisner
Helper V
Helper V

ismatch and canada zip code

I have a form where Vendors can be from the US or Canada and we need to validate users are entering the correct Zip Code format.
For US I was able to get this formula to work correctly:

If( IsMatch(VenZip.Text, Digit & Digit & Digit & Digit & Digit) Or IsMatch(VenZip.Text, Digit & Digit & Digit & Digit & Digit & Hyphen & Digit & Digit & Digit & Digit), Gray, Red)

But, per the subject of the post, for canada I also need to validate the Zip Code.
Using the site: http://www.regexlib.com/Search.aspx?k=zip I found the following expression that is for US and Canada: ^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$
which I was hoping would work, but so far no luck.. I tried:
If( IsMatch(VenZip.Text, "\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d", MatchOptions.Contains) , Gray, Red) &
If( IsMatch(VenZip.Text, "\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]", MatchOptions.Contains) , Gray, Red)
Both give me an error of:
The provided regular expression is invalid: SyntaxError: Invalid regular expression: /\ my code..... : Unmatched ')'canadazippng.png

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
EricLott
Resident Rockstar
Resident Rockstar

Try this:

If(IsMatch(VenZip.Text,"^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$",MatchOptions.Complete),Gray,Red)

It's working on my end

2020-01-09 15_35_53-SQLQuery111.sql - ubpos02_liberty.INVENTORYSCAN (OWENSGROUP_elott (64))_ - Micro.png

View solution in original post

6 REPLIES 6
EricLott
Resident Rockstar
Resident Rockstar

Try this:

If(IsMatch(VenZip.Text,"^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$",MatchOptions.Complete),Gray,Red)

It's working on my end

2020-01-09 15_35_53-SQLQuery111.sql - ubpos02_liberty.INVENTORYSCAN (OWENSGROUP_elott (64))_ - Micro.png

View solution in original post

mdevaney
Super User
Super User

@shanemeisner 

Rather than use the code you found online I would suggest this since its simpler to understand what going on.  Canadian postal codes follow the pattern A0A 0A0 where A is a letter and 0 is a number.

 

If(IsMatch(VenZip.Text, Letter & Digit & Letter & Space & Digit & Letter & Digit), Gray, Red)

 

Source: I live in Canada, LOL

---
Please click "Accept as Solution" if my post answered your question so that others may find it more quickly. If you found this post helpful consider giving it a "Thumbs Up." 

Worked perfectly  can enter 56789 or 56789-3456 or P0X 1K0 and they all work.. Any other format gives me the "RED" border error.

THANKS!!!!

JayashreeKrish
Resolver II
Resolver II

I have created a sample canvas app using sharepoint list,
In my app, ZIP is a mandatory field
 
If I give the below command , border color doesn't change to red when I leave it as empty (it only validates the zip format is correct or not )
If(
Not(IsBlank(DataCardValue5.Text)),
If(
IsMatch(DataCardValue5.Text,"^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$",MatchOptions.Complete),
Black,
Red
),
Black
)
JayashreeKrish_0-1628009094666.jpeg

 

 

 
If I just give
 
If(IsMatch(DataCardValue5.Text,"^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$",MatchOptions.Complete), Black, Red) ,
or
If(IsMatch(DataCardValue5.Text,"^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$",MatchOptions.Complete) && IsBlank(Parent.Error) , Black, Red)
 
By default, On load it is always red
JayashreeKrish_1-1628009094670.jpeg

 

 

 
But I want it to be
Black : on load and if format is correct and
red:  if it is blank or it doesnt match the validation ,SO How do I achieve this ? Can u pls help me with this
shanemeisner
Helper V
Helper V

@JayashreeKrish I always set my required fields to Red whenever they start a new item.. It is easier for user to know they must enter data before they can submit, rather then error out and then find the field that needs data.

Here is what has been working for me in multiple apps:

BorderColor - 

Zip - Border Color( where "VendorZipVal" is what I renamed my field in PowerApps):


If( (IsBlank( VendorZipVal) Or !IsMatch(VendorZipVal.Text,"^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$") ) , Red, Parent.BorderColor)

 

Zipcode datacard Required:
If( (IsBlank( VendorZipVal) Or !IsMatch(VendorZipVal.Text,"^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$") ) , true, false)

JayashreeKrish
Resolver II
Resolver II

Thanks @shanemeisner  

But in my case , ON LOAD of the form the ZIpCode field should be black

It should change to RED -> only when its blank ON SUBMIT and format doesnt matches.

 

 

If I use the above code u had shared (as u mentioned) its RED during ONLOAD and doesnt help my situation.

Please let me know of other alternatives

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

Users online (1,780)