cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

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

mdevaney
Super User
Super User

@Anonymous 

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." 

Anonymous
Not applicable

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
Anonymous
Not applicable

@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 @Anonymous  

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
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power Platform Call June 2022 768x460.png

Power Platform Community Call

Join us for the next call on June 15, 2022 at 8am PDT.

PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

Top Solution Authors
Top Kudoed Authors
Users online (2,186)