RegEx for City, State and Zip

User 2445218 Photo


Registered User
5 posts

I'm working with the web form builder version 2.3, build 5217 and trying to build a custom regex for City, State and Zip and I would like all three on a single line in the form. I can get the City and State to work and or the zip code to work including the zip + 4 . When I combine the two regular expressions together I start having code errors and other troubles.

Here is the regex for the city, state ; /^[A-Za-z . ,'-]+$/

Here is the regex for the zip +4; /^\d{5}(-\d{4})?$/


I hope I have explained this well enough for you to understand what I want to do

Thanks
Dale...
User 2147626 Photo


Ambassador
2,958 posts

I'm not a particular fan of RegEx, yet.

You are aware that WFB already includes a RegEx for US Zip Codes though it does not include the +4. WFB also has a built in list of US States to choose from, so really, the only RegEx you need to add would be for the City.

If you include a 'List Box' for the State, then select properties, then look under the options box you'll see a +- and a couple of arrows. Beside those you'll see a list box icon. Click on that and you'll get a list of options you can have. Select US-States and you're all set.

If that's not what you need I'm sure someone else will jump in here soon and may be able to help you out. Good luck.
Graphics for the web, email, blogs and more!
-------------------------------------
https://sadduck.com
User 187934 Photo


Senior Advisor
20,196 posts

Give this a try.

/^[\w\s]+,\s\w{2}\s\d{5}-\d{4}$/
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2445218 Photo


Registered User
5 posts

Hi Eric....

I tried your example and could not get it to work. I went back to my two pieces of sample code and tested them one at a time. They worked just as I thought they would. I played with combining them again and came up with this:
$string_exp = "/^[A-Za-z . ,'-]+\d{5}(-\d{4})?$/";
While the format looks wrong in the center with the
+/d
it does work. I can enter City, State and or just the Zip and get the expected error code. Then I enter the City, State and Zip or the Zip +4 and I get zero error messages....

In looking at your code, I was totality lost except for the part at the end. That is where I could see the part for the zip code. I'm glad there are places like this around. I love the web design side and doing graphic pieces. I have never fully understood any of the programming languages. I have taught my self enough PHP to make a few mail forms and when I get stuck I fall back in the CC Web form Builder....

Thanks Again Dale....
User 187934 Photo


Senior Advisor
20,196 posts

Yours didn't work for me. I actually didn't even try mine in the form builder until now. There's a bug that's preventing the validation for Regex from being generated correctly.
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 2445218 Photo


Registered User
5 posts

Hi Eric....

I was on the right track with with what I was doing. It took a couple of hours to figure out my errors and I have my sample form up and running. Here is the final code I ended up running:

$string_exp = "/^[A-Za-z . ,'-]+\d{5}(-\d{4})?$/";


I even added an Anti-Spam question using information I found on freecontactform.com.

I'm glad I'm not a programmer, I would be bald in less than a month.... :D

Thanks
Dale




Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.