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 2

  • Read Part 1

  • In Part 1 we looked at creating a basic form in an ASP.NET page. In this part we'll examine adding some of the validator controls!

    - continued -

    Now that we have our three form fields, we can go ahead and add the validators. We'll start by adding the RequiredFieldValidator to each of the three form fields. The RequiredFieldValidator takes the following form:

    <asp:RequiredFieldValidator runat="server"
          ControlToValidate="Control to Validate"
          ErrorMessage="Message to display for invalid data..."
          display="Dynamic" />

    That being done, we can go ahead and add the RequiredFieldValidator to each form field. The validator should go right next to the form field textbox, like so:

      <form method="post" action="validation.aspx" runat="server">
      <TABLE BORDER=0>
      <!--Name textbox and validation-->
        <asp:Textbox id="txtName" runat="server"/>
        <asp:RequiredFieldValidator runat="server"
          id="reqName" ControlToValidate="txtName"
          ErrorMessage = "You must provide your name!"
          display="Dynamic" />
      <!--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" />
      <!--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:button type="submit" name="btnSubmit"
                    text="Submit Form!" runat="server"/>

    Note that I chose to prefix each RequiredFieldValidator control with the letters: req. For the RangeValidator I chose to use range as the prefix and for the RegularExpressionValidator I chose to use regexp.

    Now that we've added the RequiredFieldValidator controls we're ready to add on the RegularExpressionValidator control! We'll look at accomplishing this task in Part 3!

  • Read Part 3!

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