ASP.NET RegularExpressionValidator - Validate U.S. Zip Code

RegularExpressionValidator to validate US Zip Code
The RegularExpressionValidator control validates whether the value of an associated input control matches the pattern specified by a regular expression.

The following asp.net c# tutorial code demonstrates how we can validate a US Zip Code. In this example, we will validate the United States Zip Code with RegularExpressionValidator web server control.

The RegularExpressionValidator web server control checks whether the value of an input control such as TextBox matches a pattern defined by a regular expression. This type of validation allows the asp.net developers to check for predictable sequences of characters, such as those in Zip Codes, telephone numbers, and URLs.

If the input control such as TextBox is empty, the regular expression validation will succeed. So, if a value is required for the associated input control (TextBox), the asp.net developers have to use a RequiredFieldValidator web server control in addition to the RegularExpressionValidator web server control.

The RegularExpressionValidator ValidationExpression property allows us to get or set the regular expression that determines the pattern used to validate an input field such as a TextBox. The ValidationExpression property value is a String that specifies the regular expression used to validate an input field for format. The default value of this property is Empty. The ValidationExpression property throws HttpException if the regular expression is not properly formed.

So finally, using RegularExpressionValidator control the asp.net c# developers can validate a US Zip Code. They just have to pass the specified regular expression to the ValidationExpression property value. The developers also have to use a RequiredFieldValidator control to make the input TextBox control required.
USZipCodeValidation.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e)
    { 
        if(!this.IsPostBack)
        {
            Label1.Font.Bold = true;
            Label1.Font.Italic = true;
            Label1.Font.Size = FontUnit.Large;
            Label1.ForeColor = System.Drawing.Color.HotPink;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Your Zip Code: " + TextBox1.Text.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net RegularExpressionValidator example: how to validate U.S. Zip Code</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Teal">RegularExpressionValidator: U.S. Zip Code</h2>
        <asp:Label 
             ID="Label1" 
             runat="server"
             >
        </asp:Label>
        <br /><br />
        <asp:Label 
             ID="Label2" 
             runat="server" 
             Text="U.S. Zip Code"
             Font-Bold="true"
             ForeColor="DodgerBlue"
             >
        </asp:Label>
        <asp:TextBox 
             ID="TextBox1" 
             runat="server"
             BackColor="DodgerBlue"
             ForeColor="AliceBlue"
             >
        </asp:TextBox>
        <asp:RequiredFieldValidator 
             ID="RequiredFieldValidator1"
             runat="server"
             ControlToValidate="TextBox1"
             Text="*"
             >
        </asp:RequiredFieldValidator>
        <asp:RegularExpressionValidator 
            ID="RegularExpressionValidator1"
            runat="server" 
            ValidationExpression="\d{5}(-\d{4})?"
            ControlToValidate="TextBox1"
            ErrorMessage="Input valid U.S. Zip Code!"
            ></asp:RegularExpressionValidator>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             Text="Submit Zip Code"
             Font-Bold="true"
             ForeColor="Crimson" 
             OnClick="Button1_Click"
             />
    </div>
    </form>
</body>
</html>
More asp.net examples