# Validate\_format\_of

**URL:** <https://www.ruby-forum.com/t/validate-format-of/110110>\
**Category:** Rails\
**Created:** [July 23, 2007, 11:37am UTC](https://www.ruby-forum.com/t/validate-format-of/110110 "2007-07-23T11:37:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 23, 2007, 11:37am UTC](https://www.ruby-forum.com/t/validate-format-of/110110/1 "2007-07-23T11:37:33Z")

</div>

Hi to all,

i’m using the validate\_format\_of function…

i need to check if an address is inserted in the correct way.  
I mean that the address must have this sintax:

street namestreet  
square namesquare  
etc…

I’ve looked some example with email address but i didn’t understand it.

Anybody knows how can i do??

thanks in advance

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 23, 2007, 12:24pm UTC](https://www.ruby-forum.com/t/validate-format-of/110110/2 "2007-07-23T12:24:45Z")

</div>

Hi, can you provide an actual example? For example,  
P.O. Box 222  
Santa Monica, CA 99999

1234 W 37th Street  
Los A., CA 93200

1234 W Jefferson Blvd  
Apt. 6  
Culver City, CA 90232

Now, does the above addresses represent the type of input you’re  
expecting?  
If so, then you should have similar column names:

address1  
address2  
location\_no (Apt/Suite/…)  
city  
state  
zipcode ( 5 + 4 )

If not, please provide an example of the address that you’re attempting  
to  
write a regular expression for. OK?

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 23, 2007, 1:46pm UTC](https://www.ruby-forum.com/t/validate-format-of/110110/3 "2007-07-23T13:46:01Z")

</div>

Hi Conrad,

some good input could be:

via milano

piazza duomo

🙂 it’s in the italian way!!

only the name without numers or ZIP code

thanks

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 23, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/validate-format-of/110110/4 "2007-07-23T15:18:56Z")

</div>

> validates\_presence\_of :street, :postal\_code, :city  
> validates\_format\_of :postal\_code. :with =\> /^[0-9]{5}$/
> 
> You should be able to add other validations as needed. Let me know if  
> this  
> is what you were looking to do.

Hi again Conrad,

for the postal code is great, but i want the same thing with the  
 field.  
I mean that i want to check if the user insert the value in the correct  
way without any kind of error.  
I’ll give u some example to explain it better…

ViaFrancesco Saverio can’t be good because there isn’t the space

Vai Francesco Saverio has a digit error “vai” and not “via”

thanks

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 23, 2007, 2:53pm UTC](https://www.ruby-forum.com/t/validate-format-of/110110/5 "2007-07-23T14:53:43Z")

</div>

Hi Andrea, are you creating fields for each part of the address? If so,  
what are those fields and what should go into each? OK, I found an  
example,  
Via Francesco Saverio Nitti, 28  
00156 Roma

Thus, you should be able to use the following for the above:

 ()

I’m guessing that it would be easiest to input the address as follows  
using  
the above as an example:

= Via Francesco Saverio  
 = 28  
 = 00156  
 = Roma

Now, let’s add some validations within the model:

validates\_presence\_of :street, :postal\_code, :city  
validates\_format\_of :postal\_code. :with =\> /^[0-9]{5}$/

You should be able to add other validations as needed. Let me know if  
this  
is what you were looking to do.

-Conrad

---

<div class="post-metadata">

**Author:** ![qwerty73](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/9d8465/32.png) [@qwerty73](https://www.ruby-forum.com/u/qwerty73)\
**Post date:** [July 23, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/validate-format-of/110110/6 "2007-07-23T15:58:59Z")

</div>

Hi Andrea, I see what you’re talking about now. Is there a database  
that  
contains all the valid street names? If this is the case, then one  
could do  
a live search for the street field to set it or you can write a validate  
method that checks the street name against the database. If not, then  
you’ll have to implement a validate method to verify that the street  
name is  
correct based on the rules for Italy street names. Lastly, you might  
write  
the validate method verify that the entire address is correct. This  
would  
be equivalent to an address verification service for Italy. Guess what?  
Here’s a web service that verifies an address:

> **[Data as a Service (DaaS) Provider Company | Informatica](https://www.informatica.com/products/data-quality/data-as-a-service.html)**
>
> Informatica is a data as a service (DaaS) provider that helps companies confidently engage with their customers using verified and enriched data.

Also, it very very easy to use a web service from Rails. If you need  
information on this, please let me know and I will be glad to help.

-Conrad
