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 3


  • Read Part 1
  • Read Part 2

  • In Part 2 we looked at adding the RequiredFieldValidator validator control for each of the three form fields. In this part we'll look at adding other validators!

    - continued -

    At this point we're ready to add the RegularExpressionValidator on the SSN and Age form fields. The RegularExpressionValidator validator control takes the following form:

    <asp:RegularExpressionValidator runat="server"
         id="ValidatorName"
         ControlToValidate="ControlToValidate"
         ValidationExpression="RegularExpression"
         ErrorMessage="Message to display for invalid data..."
         Display="Dynamic" />
    

    The most important part of this validator control is to choose the right regular expression. If you are unfamiliar with regular expressions I highly recommend that you read through the articles in our Regular Expressions Article Index. That being done, we can go ahead and add the RegularExpressionValidator to each form field. The validator should go right next to the form field textbox, like so:

       ...
    
      <!--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" />
        <asp:RegularExpressionValidator runat="server"
          id="regexpAge" ControlToValidate="txtAge"
          ValidationExpression="^\d{1,3}$"
          ErrorMessage = "Your age must be one to three digits."
          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" />
        <asp:RegularExpressionValidator runat="server"
          id="regexpSSN" ControlToValidate="txtSSN"
          ValidationExpression="^\d{3}\-\d{2}\-\d{4}$"
          ErrorMessage = "Your Social Seucrity Number must be in the format: XXX-XX-XXXX"
          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>
    

    The Age RegularExpressionValidator contains a regular expression that ensures the entry is one, two, or three digits. The SSN RegularExpressionValidator ensures that the SSN is in the format: XXX-XX-XXXX, where each X is a digit.

    In Part 4 we'll look at adding the final validator - a RangeValidator - and tying up all of the loose ends!

  • Read Part 4!


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