Tuesday, 4 December 2012

asp.net RegularExpressionValidator example: how to validate U.S. Zip Code


 
This example explains how to validate US ZIP Code using RegularExpressions in C#, UK Postal Code,Canada Postal Code, and Indian Postal Code. 
asp.net RegularExpressionValidator example:
how to validate U.S. Zip Code 
asp.net RegularExpressionValidator example:
how to validate Indian Postal Code
asp.net RegularExpressionValidator example:
how to validate Canada Postal Code
asp.net RegularExpressionValidator example:
how to validate UK postal Code 

This example explains how to implement postal codes Validations for various
countries in a single webpage using Validation Groups and regular expressions


<div>
         
        <asp:BulletedList ID="BulletedList1" runat="server" BackColor="#666699" BulletStyle="UpperRoman" ForeColor="White">
            <asp:ListItem>US ZIP CODE Validation using Regular Expressions C#</asp:ListItem>
            <asp:ListItem>INDIAN POST CODE Validation using Regular Expressions C#</asp:ListItem>
            <asp:ListItem>CANADA POST CODE Validation using Regular Expressions C#</asp:ListItem>
            <asp:ListItem>UK POST CODE Validation using Regular Expressions C#</asp:ListItem>
        </asp:BulletedList>

        <asp:Label ID="Label1" runat="server" Text="Enter US ZipCode"></asp:Label>
        <asp:TextBox ID="txtUSZipCode" runat="server" Height="18px" Width="263px" ValidationGroup="usa"></asp:TextBox>
        <asp:Button ID="btnValidate" runat="server" Text="Validate ZIP Code" OnClick="btnValidate_Click" />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ValidationGroup="usa" runat="server" ControlToValidate="txtUSZipCode" 
            ErrorMessage="zip code format:xxxxx(-xxxx)" ValidationExpression="\d{5}(-\d{4})?" Display="Dynamic" BackColor="#FF0066"></asp:RegularExpressionValidator>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Enter India Postal Code"></asp:Label>
        <asp:TextBox ID="txtIndianPostCode" runat="server" Height="18px" Width="263px" ValidationGroup="india"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Validate Indian postal Code" OnClick="btnValidate_Click" />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator2" ValidationGroup="india" runat="server" ControlToValidate="txtIndianPostCode" 
            ErrorMessage="Indian Post Code format:xxxxxx" ValidationExpression="\d{6}" Display="Dynamic" BackColor="Red"></asp:RegularExpressionValidator>
        <br />
         <asp:Label ID="Label3" runat="server" Text="Enter Canada Postal Code"></asp:Label>
        <asp:TextBox ID="txtcanadapostalcode" runat="server" Height="18px" Width="263px" ValidationGroup="canada"></asp:TextBox>
        <asp:Button ID="Button2" runat="server" Text="Validate canada PostalCode" OnClick="btnValidate_Click" />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator3" ValidationGroup="canada" runat="server" ControlToValidate="txtcanadapostalcode" 
            ErrorMessage="Canada Post Code format:A1A 0A0" ValidationExpression="[a-zA-Z]\d[a-zA-Z]\s\d[a-zA-Z]\d"Display="Dynamic" BackColor="Red"></asp:RegularExpressionValidator>
        <br />
         <asp:Label ID="Label5" runat="server" Text="Enter UK Postal Code"></asp:Label>
        <asp:TextBox ID="txtUKPostalCode" runat="server" Height="18px" Width="263px" ValidationGroup="UK"></asp:TextBox>
        <asp:Button ID="Button3" runat="server" Text="Validate UK PostalCode" OnClick="btnValidate_Click" />
        <asp:RegularExpressionValidator ID="RegularExpressionValidator4" ValidationGroup="UK" runat="server" ControlToValidate="txtUKPostalCode" 
            ErrorMessage="UK WC  Post Code format:AA9A 9AA" ValidationExpression="[a-zA-Z]{2}\d[a-zA-Z]\s\d[a-zA-Z]{2}" Display="Dynamic" BackColor="#CC0000"></asp:RegularExpressionValidator>
        <br />

        <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
    </div>


        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.ScriptResourceMapping.AddDefinition("jquery", new ScriptResourceDefinition
            {
                Path = "~/scripts/jquery-1.4.1.min.js",
                DebugPath = "~/scripts/jquery-1.4.1.js",
                CdnPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.min.js",
                CdnDebugPath = "http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1.js"
            });
            Page.Validate("usa");  //validatation group usa controls validation
            Page.Validate("canada"); //validatation group canada controls validation 
            Page.Validate("india"); //validatation group india controls validation
            Page.Validate("UK");//validatation group UK controls validation
            if (Page.IsValid)
            {
                Label4.Text = "Page is Valid";
            }
            else Label4.Text = "Page is not Valid!";

        }
 
output