To read the article online, visit

Form Validation with ASP.NET - It Doesn't Get Any Easier!, Part 3

  • Read Part 1
  • Read Part 2

  • In Part 2 we looked at adding the RequiredFieldValidator validator control for each of the three form fields. In this part we'll look at adding other validators!

    At this point we're ready to add the RegularExpressionValidator on the SSN and Age form fields. The RegularExpressionValidator validator control takes the following form:

    <asp:RegularExpressionValidator runat="server"
         ErrorMessage="Message to display for invalid data..."
         Display="Dynamic" />

    The most important part of this validator control is to choose the right regular expression. If you are unfamiliar with regular expressions I highly recommend that you read through the articles in our Regular Expressions Article Index. That being done, we can go ahead and add the RegularExpressionValidator to each form field. The validator should go right next to the form field textbox, like so:

      <!--Age textbox and validation-->
        <input type="text" id="txtAge" runat="server"/>
        <asp:RequiredFieldValidator runat="server"
          id="reqAge" ControlToValidate="txtAge"
          ErrorMessage = "You must provide your age!"
          display="Dynamic" />
        <asp:RegularExpressionValidator runat="server"
          id="regexpAge" ControlToValidate="txtAge"
          ErrorMessage = "Your age must be one to three digits."
          Display="Dynamic" />
      <!--SSN textbox and validation-->
        <B>Social Security Number:</B>
        <input type="text" id="txtSSN" runat="server"/>
        <asp:RequiredFieldValidator runat="server"
          id="reqSSN" ControlToValidate="txtSSN"
          ErrorMessage = "You must provide your Social Security Number."
          display="Dynamic" />
        <asp:RegularExpressionValidator runat="server"
          id="regexpSSN" ControlToValidate="txtSSN"
          ErrorMessage = "Your Social Seucrity Number must be in the format: XXX-XX-XXXX"
          Display="Dynamic" />
        <asp:button type="submit" name="btnSubmit"
                    text="Submit Form!" runat="server"/>

    The Age RegularExpressionValidator contains a regular expression that ensures the entry is one, two, or three digits. The SSN RegularExpressionValidator ensures that the SSN is in the format: XXX-XX-XXXX, where each X is a digit.

    In Part 4 we'll look at adding the final validator - a RangeValidator - and tying up all of the loose ends!

  • Read Part 4!

  • Article Information
    Article Title: Form Validation with ASP.NET - It Doesn't Get Any Easier!, Part 3
    Article Author: Scott Mitchell
    Published Date: Saturday, September 02, 2000
    Article URL:

    Copyright 2018 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers