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 ')'
Solved! Go to Solution.
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
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
@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."
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 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)
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
The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.
User | Count |
---|---|
208 | |
97 | |
60 | |
54 | |
52 |
User | Count |
---|---|
257 | |
160 | |
87 | |
79 | |
65 |