When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
Published: Saturday, September 02, 2000

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!

    - continued -

    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!

  • ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article