TheTXI
TheTXI

Reputation: 37905

Most Efficient/Elegant Address Validation

I made this small location class so that I can better handle address information which I am going to be sending off in requests to the Google Maps API. One thing I have left to do is some validation to make sure that the address has enough information to return a result back.

For the application, the level of accuracy should be as loose as a single City (meaning that it should work as long as a zip code or city/state is provided since it will find the geographic center of that area automatically in Google Maps).

So things like:

Should all work, while something like

Would not work because it would either return potentially numerous results or would put the geographic center outside my desired scope of precision.

NOTE I am sure I could list dozens of other combinations that work and do not work, but I hope I gave enough examples to properly outline the "scope of precision" here.

Public Class Location
    Private _Address1 As String
    Private _Address2 As String
    Private _City As String
    Private _State As String
    Private _ZIP As String

    Public Function isValid() As Boolean   
        'Insert validation code here'
    End Function

End Class

I know that you can make this using a whole gaggle of If statements checking if something exists and if something else exists, and if something else does not exist, on and on, but I feel as if that would be a huge chunk of logic code.

Is there a more efficient/elegant way of making sure I have a valid Location before I send it out in the request?

Upvotes: 1

Views: 926

Answers (2)

Ryan Brunner
Ryan Brunner

Reputation: 14851

In terms of required items, your rules really boil down to:

A valid address must have either a ZIP code or both a city and state.

As far as I can see, everything that satisfies that rule should be OK. Your validation code can be as simple as:

Public Function isValid() As Boolean   
    If (String.IsNullOrEmpty(city) or String.IsNullOrEmpty(state)) And _
        String.IsNullOrEmpty(zip) Then

        Return False
    Else
        Return True
    End If
End Function

Obviously, you'll probably also want to check that the values within the field conform to expectations (proper ZIP length, valid state, etc.)

As an aside, you might want to consider returning something more than a boolean from the IsValid check. If the result is valid, a simple true will suffice, but often with false return values you want to know why it is not valid. I usually do something like:

Class ValidationResult
    Public Property IsValid as Boolean
    Public Property Errors as IEnumerable<String>
End Class

Upvotes: 2

LukeH
LukeH

Reputation: 269628

Wouldn't the stated criteria just boil down to this:

Public Function isValid() As Boolean   
    Return _
        Not String.IsNullOrEmpty(_ZIP) _
        Or Not (String.IsNullOrEmpty(_City) Or String.IsNullOrEmpty(_State))
End Function

Upvotes: 1

Related Questions