When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles [1.x] [2.0]
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips
Search

Sections:
Book Reviews
Sample Chapters
Commonly Asked Message Board Questions
Headlines from ASPWire.com
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
Web Hosts
XML Info
Information:
Advertise
Feedback
Author an Article
Technology Jobs



















internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers
ASP ASP.NET ASP FAQs Message Board Feedback ASP Jobs
Print this page.

Senior Web Content Specialist
Aquent
US-NJ-Parsippany

Justtechjobs.com Post A Job | Post A Resume

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!


    Windows Internet Technology | ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article



  • JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers