web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / ismatch and canada zip...
Power Apps
Answered

ismatch and canada zip code

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

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

 

 

Categories:
  • Verified answer
    Community Power Platform Member Profile Picture
    Microsoft Employee on at

    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 Profile Picture
    29,993 Moderator on at

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

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    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 Profile Picture
    270 on at
    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
  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    @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 Profile Picture
    270 on at

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 352 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 323

#3
Mohsin Ali Profile Picture

Mohsin Ali 226

Last 30 days Overall leaderboard