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

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
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"
          id="ValidatorName"
          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-->
      <TR><TD ALIGN=RIGHT>
        <B>Name:</B>
      </TD><TD>
        <asp:Textbox id="txtName" runat="server"/>
      </TD><TD>
        <asp:RequiredFieldValidator runat="server"
          id="reqName" ControlToValidate="txtName"
          ErrorMessage = "You must provide your name!"
          display="Dynamic" />
      </TD></TR>
    
      <!--Age textbox and validation-->
      <TR><TD ALIGN=RIGHT>
        <B>Age:</B>
      </TD><TD>
        <input type="text" id="txtAge" runat="server"/>
      </TD><TD>
        <asp:RequiredFieldValidator runat="server"
          id="reqAge" ControlToValidate="txtAge"
          ErrorMessage = "You must provide your age!"
          display="Dynamic" />
      </TD></TR>
    
      <!--SSN textbox and validation-->
      <TR><TD ALIGN=RIGHT>
        <B>Social Security Number:</B>
      </TD><TD>
        <input type="text" id="txtSSN" runat="server"/>
      </TD><TD>
        <asp:RequiredFieldValidator runat="server"
          id="reqSSN" ControlToValidate="txtSSN"
          ErrorMessage = "You must provide your Social Security Number."
          display="Dynamic" />
      </TD></TR>
    
      <TR><TD COLSPAN=3 ALIGN=CENTER>
        <asp:button type="submit" name="btnSubmit"
                    onclick="btnSubmit_OnClick"
                    text="Submit Form!" runat="server"/>
      </TD></TR>
      </TABLE>
      </form>
    

    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] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article