domingo, 23 de septiembre de 2012

Validacion de Direcciones Postales de USA en ASP.NET

Para este proyecto descargaremos la dll llamada USPSAddressValidator y la agregamos como referencia a nuestro proyecto. Ademas crearemos 2 paginas de nombre AddressValidator y AddressVerification. En el primero lo diseñamos de la siguiente forma:
Ahora el codigo del cliente tendra este codigo(AddressValidator.aspx):
<head runat="server">
    <title>Address Verification For USA</title>
    <script type="text/javascript">


        function YahooWindow() {
            var street = document.getElementById('<%=txtAddress.ClientID %>').value;
            var city = document.getElementById('<%=txtCity1.ClientID %>').value;
            var e = document.getElementById('<%=ddlState.ClientID%>');
            //For ModelWindow then uncomment below code
//            $("#Address1").fancybox({
//                'width': '55%',
//                'height': '30%',
//                'autoScale': false,
//                'transitionIn': 'none',
//                'transitionOut': 'none',
//                'type': 'iframe'
//            });
            var strV = e.options[e.selectedIndex].text;
            //alert(city);
            document.getElementById('Address2').href = 'AddressVerification.aspx?street=' + street + '&city=' + city + '&state=' + strV + ''
            //yahoowin = dhtmlmodal.open('yahoo', 'iframe', 'AddressVerification.aspx?street=' + street + '&city=' + city + '&state=' + strV + '', 'Address Verification', 'width=400px,height=350px,center=1');
            //             $("#Address1").click();
            //             return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td class="sub02" width="180px">
                    Address:
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtAddress" CssClass="input" />
                    <a href="#" id="Address2" onclick="return YahooWindow();">[Verify Address] </a><a
                        href="#" id="Address1"></a>
                
                </td>
            </tr>
            <tr>
                <td class="sub02" width="180px">
                    City:
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtCity1" CssClass="input" />
                   
                </td>
            </tr>
            <tr>
                <td class="sub02" width="180px">
                    State:
                </td>
                <td>
                    <asp:DropDownList runat="server" ID="ddlState" CssClass="input">
                        <asp:ListItem>--Select State--</asp:ListItem>
                        <asp:ListItem>AK</asp:ListItem>
                        <asp:ListItem>AL</asp:ListItem>
                        <asp:ListItem>AR</asp:ListItem>
                        <asp:ListItem>AZ</asp:ListItem>
                        <asp:ListItem>CA</asp:ListItem>
                        <asp:ListItem>CO</asp:ListItem>
                        <asp:ListItem>CT</asp:ListItem>
                        <asp:ListItem>DC</asp:ListItem>
                        <asp:ListItem>DE</asp:ListItem>
                        <asp:ListItem>FL</asp:ListItem>
                        <asp:ListItem>GA</asp:ListItem>
                        <asp:ListItem>HI</asp:ListItem>
                        <asp:ListItem>IA</asp:ListItem>
                        <asp:ListItem>ID</asp:ListItem>
                        <asp:ListItem>IL</asp:ListItem>
                        <asp:ListItem>IN</asp:ListItem>
                        <asp:ListItem>KS</asp:ListItem>
                        <asp:ListItem>KY</asp:ListItem>
                        <asp:ListItem>LA</asp:ListItem>
                        <asp:ListItem>MA</asp:ListItem>
                        <asp:ListItem>MD</asp:ListItem>
                        <asp:ListItem>ME</asp:ListItem>
                        <asp:ListItem>MI</asp:ListItem>
                        <asp:ListItem>MN</asp:ListItem>
                        <asp:ListItem>MO</asp:ListItem>
                        <asp:ListItem>MS</asp:ListItem>
                        <asp:ListItem>MT</asp:ListItem>
                        <asp:ListItem>NC</asp:ListItem>
                        <asp:ListItem>ND</asp:ListItem>
                        <asp:ListItem>NE</asp:ListItem>
                        <asp:ListItem>NH</asp:ListItem>
                        <asp:ListItem>NJ</asp:ListItem>
                        <asp:ListItem>NM</asp:ListItem>
                        <asp:ListItem>NV</asp:ListItem>
                        <asp:ListItem>NY</asp:ListItem>
                        <asp:ListItem>OH</asp:ListItem>
                        <asp:ListItem>OK</asp:ListItem>
                        <asp:ListItem>OR</asp:ListItem>
                        <asp:ListItem>PA</asp:ListItem>
                        <asp:ListItem>RI</asp:ListItem>
                        <asp:ListItem>SC</asp:ListItem>
                        <asp:ListItem>SD</asp:ListItem>
                        <asp:ListItem>TN</asp:ListItem>
                        <asp:ListItem>TX</asp:ListItem>
                        <asp:ListItem>UT</asp:ListItem>
                        <asp:ListItem>VA</asp:ListItem>
                        <asp:ListItem>VT</asp:ListItem>
                        <asp:ListItem>WA</asp:ListItem>
                        <asp:ListItem>WI</asp:ListItem>
                        <asp:ListItem>WV</asp:ListItem>
                        <asp:ListItem>WY</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="sub02" width="180px">
                    Zip:
                </td>
                <td>
                    <asp:TextBox runat="server" ID="txtZipCode" CssClass="input" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
El diseño de nuestro 2 webform el cual nos mostrara si la direccion es valida sera el siguiente:
El codigo del lado del cliente sera este(Addressverification.aspx)
<head runat="server">
    <title></title>
    <style type="text/css">
    body{ margin:0;
          padding:0;
          font-family:Arial, helventica, Verdana;
          font-size:11px;
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div  style="text-align: left; vertical-align: middle;">
    
    <asp:Image runat="server" id="imgNo" ImageAlign="AbsMiddle" 
            ImageUrl="~/images/cancel_128x128.png" Height="65px" Width="71px"></asp:Image>
    <asp:Image runat="server" id="imgYes" ImageAlign="AbsMiddle" 
            ImageUrl="~/images/k10760520.jpg" Height="59px" Width="62px"></asp:Image>    
    <asp:Label runat="server" id="lblVerification"></asp:Label>    
    </div>
    <div  style="text-align: left; padding-left:75px;">
    <asp:RadioButton runat="server" id="rdo1" Text="Give me some address suggestions" GroupName="rdosug" AutoPostBack="true"></asp:RadioButton><br /> 
    <asp:RadioButton runat="server" id="rdo2" Text="The address I entered is correct" GroupName="rdosug" AutoPostBack="true"></asp:RadioButton> <br /> 
    <asp:RadioButton runat="server" id="rdo3" Text="I will make my address corrections  " GroupName="rdosug" AutoPostBack="true"></asp:RadioButton>    
    </div>
    </form>
</body>
En el codigo del lado del servidor escriben lo siguiente(AddressVerification.aspx.vb)
Imports System.Web.UI.HtmlControls
Imports System.Configuration
Imports System.Net
Partial Class AddressVerification
    Inherits System.Web.UI.Page
    Private Function ValidateAddress(ByVal street As String, ByVal city As String, ByVal state As String) As String
        If ConfigurationManager.AppSettings("proxy") <> [String].Empty Then
            Dim p As WebProxy = Nothing
            Dim proxyAddressAndPort As String = ConfigurationManager.AppSettings("proxy")
            Dim proxyUserName As String = ConfigurationManager.AppSettings("proxyUserName")
            Dim proxyPassword As String = ConfigurationManager.AppSettings("proxyPassword")
            Dim cred As ICredentials
            cred = New NetworkCredential(proxyUserName, proxyPassword)
            p = New WebProxy(proxyAddressAndPort, True, Nothing, cred)
            GlobalProxySelection.[Select] = p
        End If
        If street = String.Empty Then
            street = "Test"
        End If
        If city = String.Empty Then
            city = "Test"
        End If
        If state = "--Select State--" Then
            state = "TA"
        End If
        Dim res As String = PAB.Utils.USPS.AddressValidator.ValidateAddress(street, city, state)
        Return res
    End Function

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Request("city") Is Nothing Then
            Dim strvalid As String = String.Empty
            strvalid = Me.ValidateAddress(Request("street"), Request("city"), Request("state"))
            If strvalid = "Invalid Address" Then
                lblVerification.Text = "We’re sorry.  We were unable to verify your address with the United States Postal Service."
                imgYes.Visible = False
            Else
                lblVerification.Text = "Congratulations!  We have successfully verified your shipping address!"
                imgNo.Visible = False
                rdo1.Visible = False
                rdo2.Visible = False
                rdo3.Visible = False

            End If
        End If
    End Sub
End Class
. finalmente en nuestro web.config modificamos nuestra etiqueta appconfig y la escribimos como esta abajo:
<appSettings>
    <add key="proxy" value="10.10.201.3:8080"/>
    <add key="proxyUserName" value=""/>
    <add key="proxyPassword" value=""/>
  </appSettings>
si pusieron todo el codigo como les indique les debe salir asi:
U.S.A Postal Address Validation
Espero les ayude este ejemplo

0 comentarios:

Publicar un comentario