how to validate a zip code in Phoenix?

I’m sorry if this is a duplicate question. Learning through Phoenix docs. I was trying to search for an answer on stack overflow and the web but couldn’t find an exact answer. Then, I looked into implementing some kind of regex but I couldn’t get that to go either. I was wondering if there is a simple way to validate something like a 5 digit zip code or a SSN without dashes in my changeset where it validates a certain amount of numbers. I was also thinking that since the value is already typed as an integer, maybe convert it to a string(?) and use validate_length(?). Not really sure.

Thanks!

Hi @yujiku808 welcome!

Can you show what you tried?

Is it an integer? That depends on what form field you have. If you do a text field it’s already just a string.

ZIP should be text. Some ZIPs start with 0s.

This is an interesting example, that loads a maintained set of regex’s from a file, and compares the postal code mapped to 2 letter country code ecto_commons/postal_code.ex at master · achedeuzot/ecto_commons · GitHub

3 Likes