Validate_format_of

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

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

Hi Conrad,

some good input could be:

via milano

piazza duomo

:slight_smile: it’s in the italian way!!

only the name without numers or ZIP code

thanks

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

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

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:

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