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

Form Validation with ASP.NET - It Doesn't Get Any Easier!


One annoying task that most every developer has had to face in the past is form validation. Since forms are an integral part of dynamic, data-driven Web sites, it is essential that a user's query into a form fit the specified guidelines. For example, in a Web site like Amazon.com where users are entering shipping and billing information, it is vital that the user enter credit card numbers, zip codes, email addresses, and other information in an acceptible format.

ASP.NET, which is currently in beta version but is planned on being "officially" released in Q1/Q2 of 2001, greatly simplifies this annoyance. (To learn more about ASP.NET, be sure to read our ASP.NET Article Index!) ASP.NET includes six validation controls that you can use to simplify the entire validation process. ASP.NET also provides a simplified post-back paradigm. In ASP, you can create post-back forms by creating a form whose ACTION property in the FORM tag has the same URL as the page that contains the form. More simply put, imagine that you had a page named /scripts/DisplayForm.asp that had the following HTML:

<FORM METHOD=POST ACTION="/scripts/DisplayForm.asp">

Note that the ACTION property is set to /scripts/DisplayForm.asp, the same URL of the ASP page displaying the form. Such a technique, referred to as a "post-back form" will cause the page to be reloaded when the user submits the form.

ASP.NET automatically will pass back the various form field elements through a post-back form. You can also setup special validator controls whose purpose is to validate a specific form field. If all of the user's entries are deemed "valid" by these form validators, the Page.IsValid property is set to True, which we'll see here shortly in our example.

Of the six ASP.NET validator controls, the three we'll examine in this article are the RequiredFieldValidator, the RangeValidator, and the RegularExpressionValidator. As their names imply, the RequiredFieldValidator should be used on required form fields, the RangeValidator should be used to ensure that the form field value falls within a certain range, and the RegularExpressionValidator ensures that the form field entry corresponds to a regular expression. (To learn more about regular expressions, be sure to visit our Regular Expressions Article Index!) You can use any number of validators on a single form field. In our example, we'll ask the user to provide three pieces of information: their Name, Age, and Social Security Number (SSN). All three fields will have the RequiredFieldValidator validator; the Age and SSN form fields will have the RegularExpressionValidator; furthermore, the Age form field will have a RangeValidator applied to it as well.

We'll use the following ASP.NET/HTML code to create our three form fields for the Name, Age, and Social Security Number information:

  <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></TR>

  <!--Age textbox and validation-->
  <TR><TD ALIGN=RIGHT>
    <B>Age:</B>
  </TD><TD>
    <input type="text" id="txtAge" runat="server"/>
  </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></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>

Notice that the ACTION property of the form is set to Validation.aspx. Since we want to make this a post-back form, that means that this code should be placed in a file named: Validation.aspx. Notice that we've used both the asp:TextBox and the input type="text" server controls. This is to simply demonstrate that textboxes, in ASP.NET, can be done both ways. Also take note of the asp:button control; this creates the SUBMIT button for the form, and, when clicked, will fire the btnSubmit_OnClick subroutine. This sub will be responsible for checking to see if all of the form fields are valid and, if they are, then taking whatever action the form intends to take!

Note the runat="server"!
The only way for an ASP.NET page to realize that you are using a server control (like asp:Textbox or input type="text") is to have runat="server" in the control tag. This is very important; forgetting it will cause the ASP.NET engine to pass over the control as HTML.

Now that we've created the form fields in our ASP.NET page, we're ready to add the validator controls. We'll look at this task in Part 2!

  • Read Part 2


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


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