To read the article online, visit http://www.4GuysFromRolla.com/webtech/090200-1.2.shtml

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!

    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!


  • Article Information
    Article Title: Form Validation with ASP.NET - It Doesn't Get Any Easier!, Part 2
    Article Author: Scott Mitchell
    Published Date: Saturday, September 02, 2000
    Article URL: http://www.4GuysFromRolla.com/webtech/090200-1.2.shtml


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