October 4, 2007
US Postal Code Validator for BVC5
Shipping Rate Provider Plug-in is a free add-on that adds postal code support to BVC5. The plug-in includes almost 43,000 US postal codes. This article demonstrates one way of using that data by creating a US postal code validator.
Before you add the validator to your site, install the plug-in and load the US postal codes. Then save PostalCodeValidator.vb to the App_Code directory of your web site.
Imports BVSoftware.Bvc5.Core.Content
Imports StructuredSolutions.Bvc5.Shipping.PostalCodes
Namespace StructuredSolutions
Public Class PostalCodeValidator
Inherits Anthem.CustomValidator
Private _countryFieldID As String = String.Empty
Public Property CountryFieldID() As String
Get
Return _countryFieldID
End Get
Set(ByVal value As String)
_countryFieldID = value
End Set
End Property
Private Shared USCountryBvin As String = Country.FindByISOCode("US").Bvin
Protected Overrides Function OnServerValidate(ByVal value As String) As Boolean
If String.IsNullOrEmpty(CountryFieldID) Then
Throw New ArgumentNullException("CountryField") End If
If String.IsNullOrEmpty(ControlToValidate) Then
Throw New ArgumentNullException("ControlToValidate") End If
UpdateAfterCallBack = True
Dim countries As DropDownList = CType(Me.NamingContainer.FindControl(CountryFieldID), _
DropDownList)
If String.Compare(countries.SelectedValue, USCountryBvin, True) = 0 Then
Dim test As PostalCode = PostalCode.FindByCode(countries.SelectedValue, value)
If String.Compare(test.Code, value, True) <> 0 Then
Return False
End If
End If
Return True
End Function
End Class
End Namespace
This will create a new custom web control called PostalCodeValidator that we can use on any web page within the site. PostalCodeValidator inherits most of its functionality from Anthem.CustomValidator, which is an updatable version of Microsoft's CustomValidator.
The real meat of the validator is in the OnServerValidate override. Here we determine if the postal code is from the US and if it is, then we verify that it exists in the postal code database. If it does not exist, then validation fails. This could easily be extended to include other countries and different validation methods. For example, you can check the format of the postal code using a regular expression.
Now that we have defined PostalCodeValidator, its time to use it on a web page. Open your checkout page with the postal code field (i.e. One Page Checkout\Checkout.aspx) and add this line to the top of the file:
<%@ Register TagPrefix="ss" Namespace="StructuredSolutions" %>
Now find your shipping postal code field, and insert this HTML:
<ss:PostalCodeValidator id="PostalCodeValidator" runat="server"
ControlToValidate="ShippingpostalCodeField"
CountryFieldID="ShippinglstCountry"
Display="Dynamic"
Text="<br />Invalid Zip Code">
</ss:PostalCodeValidator>
Where ControlToValidate is the ID of the postal code field and CountryFieldID is the ID of the shipping country dropdown list.
Now when your page is validated, the PostalCodeValidator will verify that postal code is valid. However, when I was writing this article, I noticed that One Page Checkout does not validate the page before updating the shipping rates, so I made one more change to Checkout.aspx.vb. Find the ShippingpostalCodeField_TextChanged method and replace it with this one:
Protected Sub ShippingpostalCodeField_TextChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles ShippingpostalCodeField.TextChanged
Page.Validate()
If Page.IsValid Then
UpdateShipping()
End If
End Sub
That's it.
This site looks much better in a browser that supports current web standards, but it is accessible to any browser.
Download one now
Some parts of this site will not work effectively on this older browser.
Please consider
updating your browser