4

What is the best way using C# asp.net 4.0+ to validate whether or not the address someone is giving you is viable. I guess what I am saying is I don't want to see under my address field "Wouldn't you like to know?". I would expect a address field to have a house number street maybe in a mixed order.

1623 ellis st

or

2 camden street

or

3 malberry rd apt 2b

Is it worth trying to achieve this in a class environment/job interview environment?

Blachshma
  • 17,097
  • 4
  • 58
  • 72
jackncoke
  • 2,000
  • 6
  • 25
  • 66
  • 3
    "Best" being a subjective term, probably the only reliable way is to connect to some external service to validate that the address is known. Perhaps the US Postal Service has an API? (probably not for free) Another approach might be to use the Google Geocoding API to see if it can locate the address. If it can't, there's a good chance it's not a good address, I suppose. (The acceptable margin of error is up to you.) If you're looking for a regex, that probably won't be a good idea. Addresses can vary significantly. – David Mar 27 '13 at 23:14
  • @David thank you. I cant spend money on a mock up. – jackncoke Mar 27 '13 at 23:16
  • 2
    I like David's idea, at least if the addresses are U.S. addresses. And it looks like if you register, you can get free access to the usps address verifier: https://www.usps.com/business/web-tools-apis/address-information.htm#1 . That said, it seems massively excessive unless you have a business model that explicitly calls for it. – Scott Mermelstein Mar 27 '13 at 23:21
  • 1
    the USPS API is how all the big sites do it. Take a look at stackoverflow.com/questions/682093w/address-validation-using-google-maps-api though, every situation is a little bit different and supposedly w the right use case, USPS can be free. – RandomUs1r Mar 27 '13 at 23:21
  • 2
    If you are limited to dealing with US addreses, these solutions may work, though you need to consider whether you want to accept US military APO/FPO addresses. Those really cannot be verified beyond ensuring the "city" is APO or FPO and the "state" is AA, AE, or AP. Internationally, the format of addresses may vary widely between countries, and there is really no type of validation you can do that won't exclude valid addresses. – j__m Mar 27 '13 at 23:25
  • 1
    "Is it worth trying to achieve this in a class environment/job interview environment?" It's not clear what this means - do you mean is "provide code to validate an address in C#" a good question to pose to a student or job candidate? I'd argue that attempting to rigorously validate an address is difficult enough to almost never be worth it. – Dan J Mar 27 '13 at 23:34
  • @DanJ: Completely agree. I've seen countless companies spend tons of money trying to reinvent this same wheel. In a post-Google world, an "address" is nothing more than a free text single line input. Geocode it to get the strongly typed data and be done with it. None of this "house number, street name, city, state, zip, etc., etc." nonsense. There will always be outliers to break the format. (For example, I once lived at "1838B S 3rd St. W Apt 2". Go ahead, break that into strongly typed components.) – David Mar 27 '13 at 23:40
  • 1
    @David Yep. Ben Alabaster has a great take on the rigor required to gain and store *useful* address data in [this SO answer and linked blog post](http://stackoverflow.com/a/1160031/238688). Which leads me to ask what one intends to do with address data before tackling the problem: if you're running a logistics company, the effort might be worthwhile. If you're gathering customer addresses for direct marketing, perhaps having some invalid ones isn't such a big deal. – Dan J Mar 27 '13 at 23:51

1 Answers1

4

Just a thought, but I'd say no it's not worth it. If ever I encounter a site which validates street addresses and I don't want to give an address, I'll give a different address (the company's own, or a random one from Google.) So in high fallutin' compsci terms whether you can validate the street address is irrelevant since you can't verify that the street address belongs to the person entering it (short of mailing them a code and requiring them to enter it on the site.)

El Zorko
  • 3,349
  • 2
  • 26
  • 34