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 4

  • Read Part 1
  • Read Part 2
  • Read Part 3

  • In Part 3 we looked at adding two RegularExpressionValidator validators. In this fourth and final part we'll look at adding a single RangeValidator validator and tie up all the loose ends!

    - continued -

    Time to add our last validator control, the RangeValidator, to ensure that the Age is between a valid range (we don't want someone entering an age of 2 or 739, which would pass our RegularExpressionValidator control). For this example, we'll allow ages between 5 and 120. The RangeValidator has the following form:

    <asp:RangeValidator runat="server"
          Type="Comparison Data Type"
          ErrorMessage="Message to display for invalid data..."
          Display="Dynamic" />

    The MaximumControl and MinimumControl need to reference asp:TextBox controls with the maximum and minimum ranges entered. Let's go ahead and add the RangeValidator to each form field. The validator should go right next to the form field textbox, like so:

      <!--Age textbox and validation-->
      <asp:TextBox id="minAge" text="5" runat="server" visible="False" />
      <asp:TextBox id="maxAge" text="120" runat="server" visible="False" />
        <input type="text" id="txtAge" runat="server"/>
        <asp:RequiredFieldValidator runat="server"
          id="reqAge" ControlToValidate="txtAge"
          ErrorMessage = "You must provide your age!"
          display="Dynamic" />
        <asp:RegularExpressionValidator runat="server"
          id="regexpAge" ControlToValidate="txtAge"
          ErrorMessage = "Your age must be one to three digits."
          Display="Dynamic" />
        <asp:RangeValidator runat="server" Type="Integer"
          id="rangeAge" ControlToValidate="txtAge"
          MinimumControl="minAge" MaximumControl="maxAge"
          ErrorMessage = "Your age must be between 5 and 120."
          Display="Dynamic" />

    Note the two asp:TextBox controls we added up at the top. Their Visible property is set to False so they won't show up on the Web page, but they are needed for the RangeValidator to function properly. Note that we have hard coded the values of 5 and 120 into the minAge and maxAge controls.

    Now that we have all of our validator controls in place, all that's left is the code to successfully handle the form if every form field is deemed valid. As you saw in our first code snippet, the submit button for the form is created with an asp:button. We need to create the btnSubmit_OnClick function, which we can do with the following server-side script block:

      Sub btnSubmit_OnClick(Obj as Object, E as EventArgs)
        'Determine if the form entries were valid
        If Page.IsValid then
          'This is the point where we want to handle the form.
          'Once we've "handled" the form we'll redirect the user to
          'the ThankYou.aspx page.
          'You could do a number of things here, to "handle" the form.
          'Perhaps you want to send an email to someone, or update/insert a
          'record in a database.  In any case, this is the place where you would
          'do that...
          Page.Navigate("ThankYou.aspx?Name=" & txtName.Text)   'Redirect the user
        End If
      End Sub

    Well, that's about it! While a lot of the code may be very new to you if you've not worked with ASP.NET before, hopefully it has gotten your feet wet and shown you how easy form validation is with ASP.NET!

    Happy Programming!


  • View the source for Validation.aspx in text format
  • Read the official docs on the Validator Controls
  • Visit the ASP.NET Article Index
  • Visit the Regular Expressions Article Index

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