Use asp:RegularExpressionValidator to check the zip code : Regular Expression « Validation « ASP.NET Tutorial






<html><body>
   <form runat="server">
      <asp:Label id="lblMessage" runat="server" /><br>
      <asp:Panel id="Panel1" runat="server">
         <table>
         <tr>
            <td valign="top">City, State, ZIP (5-digit):</td>
            <td valign="top">
               <asp:TextBox id="tbCity" 
                  runat="server" />,
               <asp:TextBox id="tbState" runat="server"
                  size=2 />&nbsp;
               <asp:TextBox id="tbZIP" runat="server"
                  size=5 /><br>
               <asp:RegularExpressionValidator runat="server"
                  ControlToValidate="tbZIP"
                  ValidationExpression="[0-9]{5}"
                  ErrorMessage="That is not a valid ZIP" /><br>
               <asp:RangeValidator runat="server"
                  ControlToValidate="tbZIP"
                  MinimumValue="00000" MaximumValue="22222"
                  Type="String"
                  ErrorMessage="You don't live in the correct region" />
            </td>
         </tr>

         <tr>
            <td colspan="2" valign="top" align="right">
               <asp:Button id="Submit" runat="server" 
                  text="Add" />
            </td>
         </tr>
         </table>
      </asp:Panel>
   </form>
</body></html>








8.9.Regular Expression
8.9.1.The predefined character classes
8.9.2.quantifiers used to create patterns that match a variable number of characters at a certain position in the string
8.9.3.Metacharacters for Matching Single Characters
8.9.4.Metacharacters for Matching Types of Characters
8.9.5.Quantifiers
8.9.6.Commonly Used Regular Expressions
8.9.7.RegularExpressionValidator compares value against a regular expression
8.9.8.Regular Expression based validation (C#)
8.9.9.Use asp:RegularExpressionValidator to check the zip code
8.9.10.Use asp:RegularExpressionValidator to check the email address
8.9.11.Use Regular expression to validate a Phone number