Lesson 9:
Validating User Input

Introduction

This week in Chapter 9: Validating User Input you will learn how to obtain user input from your Web applications and why it is important to validate it. This week's assignment has you creating a user control that is be used for retrieving user inputted data from  a Web form and emailing it to the destination of your choice.

You may or may not have heard the phrase "garbage in, garbage out," or its acronym "GIGO" (pronounced guy-go); this is a term used by developers which encapsulates the essence of one of the cardinal rules of data processing which is, if a coding procedure uses bad data when performing an operation then the results will be invalid. No matter what, you cannot trust users to input data correctly. This can be for many reasons, miscommunication, misunderstanding, malicious intent, or improper format (numbers instead of letters or vice versa), just to name a few. It is your job as the application developer to build bullet-proof Websites which can both withstand errors that occur during data processing as well as reducing the risks of bad data entering in to the system in the first place, that is where user validation comes in.

Assignment

In order to receive full credit for this week's assignment you must complete the following:

  • Read all of Chapter 9: Validating User Input in your text book to learn about ASP.NET validation controls, the difference between server-side and client-side validation, and how to configure your Website to use a SMTP email service.
  • Create a ContactForm user control in the Controls folder off the root of your Website using the instructions below.
  • Add and Register the ContactForm user control in your Contact Web form in the About folder off your root using the instructions below.
  • Modify the Web.config file in your root to handle the unobtrusive validation issue and to configure your Web site to use the SMTP service as explained in the instructions below.
  • Publish to the following to your student folder on the IA.MSJC.EDU Web server:
    • Web.config file from the root of your site
    • ContactForm.txt file in your App_Data folder off the root of your site
    • DarkGrey.css & Monochrome.css files from their corresponding App_Themes folders off the root of your site
    • Contact.aspx file in your About folder off the root of your site
    • ContactForm.ascx file in your Controls folder off the root of your site
  • Verify that your control can email its form values to the SMTP server (the IA.MSJC.EDU/Email folder) and that all validation controls are working.
  • Post the URL for the Contact.aspx Web form in your Website's About folder to Canvas for scoring by the due date listed in the Syllabus.
Assignment 9 completed Contact Us form.

Assignment 9 Contact Us form showing validation errors.

Instructions

Use the following step-by-step instructions for completing this week's assignment. Remember to use C# when copying code snippets from your text book.

Step 1 Complete all the steps in the Try It Out Using the RequiredFieldValidator on pages 303 - 305 to create a new user control ContactForm.ascx and add it to the Contact.aspx Web form.

Step 2 Complete all the steps in the Try It Out Extending the Contact Form on pages 311 - 314 to extend your Contact Form user control and add the appropriate validation controls.

Step 3 Complete all the steps in the Try It Out Writing Client- and Server-side Validation Methods on pages 315 - 317 to write your client-side and server-side validation methods.

Step 4 Do not complete any of the steps in the Try It Out on pages 324 - 326 Sending E-Mail Messages. Instead modify your Web.config file by adding the code shown inside the red rectangle below:

 Image of the code inserted into Web.config in step 4.
Figure 9.2: The Web.config file modification for configuring the ASP.NET mailSettings to send email to a folder on the IA.MSJC.EDU server.

TIP: Notice that the new <system.net> element's code has been added right before the closing tag of the <configuration> element.

The code above will configure your Website to deliver SMTP mail to a folder on the IA.MSJC.EDU server. It is a folder created for this class and its permissions are set so that all CSIS 786 student Websites can write files to it. There may be a lot of other emails posted before yours, to locate yours, use the timestamp to the left of the filename. Yours should be the most recent email posted.

Step 5 Complete all the steps in the Try It Out Sending Mail from the ContactForm User Control on pages 328 - 330 to send mail from the ContactForm User control.

Step 6 Publish the following to your student folder on the Student Web server.

  • Web.config file from the root of your site
  • ContactForm.txt file in your App_Data folder off the root of your site
  • DarkGrey.css & Monochrome.css files from their corresponding App_Themes folders off the root of your site
  • Contact.aspx file in your About folder off the root of your site
  • ContactForm.ascx file in your Controls folder off the root of your site

Don't publish any other files or folders at this time.

Step 7 Using a Web browser on your computer, verify that all of the validation controls are working and that you can successfully email the contents of your ContactForm control to the Email folder on the IA.MSJC.EDU server (or using a specified SMTP server - see Extra Credit below) and that the contents of the email displays all the values entered on your ContactForm control - notice how your values where merged with your text file to create the message body in the email.

e.g. type http://IA.MSJC.EDU/7-digitStudentIDNumber/About/Contact.aspx into your Web browser. Try sending your form, it should fail and display the errors which need correcting in red text under the form. Fill-in each TextBox one-by-one clicking Send as you enter each one until your form is completed - remember to also type in a bad email address. Once your form is complete and free of errors, click Send one more time. This time your form should disappear and you should see the text "Message Sent" in place of the form.

Use the instructions below to view and then, finally, delete your eml file.

Viewing Your Email File

To access your email after a successful send from your website, type IA.MSJC.EDU/email in your Web browser (be sure that your are connected to the AWS VPN first). If your send mail was successful you should see your "eml" file in your Web browser's window. You can download the eml file to your computer by clicking on the filename. Use an ascii text editor like Notepad to view the downloaded eml file - you will see it is a very basic ASCII text file which includes the information from the TextBoxes in your ContactForm control sent from your Contact form. The format you are viewing is the same format an email takes when it is sent using SMTP.

Deleting Your Email File

Using the method stated in the last paragraph to view your email does not let you delete your email. To delete your email you need to use an FTP client like Windows Explorer or File Explorer in Windows 8 and 10.

Type ftp://ia.msjc.edu/email in the address bar. Use your CSIS 786 student credentials to login. Once the window appears displaying your email you should be able to delete it by selecting it and then pressing delete on your keyboard.

Once you have you have successfully completed the steps listed above, you are ready to complete step 8 below.

Step 8 Post the URL for your Contact.aspx Web form to Canvas for scoring by the due date listed in the Syllabus for Assignment 9.

Important - Please Read: When I test your submitted URLs for this assignment the first thing I will do is delete any files I find in the IA.MSJC.EDU/email folder, then I will complete your Contact Web form and try to send its contents via email using the Send button on your Contact Web form. The send email step must complete successfully for you to get full credit for this assignment!

Please email me if you have any questions at all!

Lesson Summary

In this lesson you learned how to:

  • How to accept user input and the importance to validate it
  • Use ASP.NET controls to aid in validating user input
  • Work with the built-in validation controls and how to create solutions that are not supported out of the box
  • Send e-mail using ASP.NET
  • Read text files

Congratulations, that's it for this assignment. You can get a head start on your next lesson by reading chapter 10 in your textbook.

Instructor's Note: Everything MUST be completed before the due date listed in the syllabus for this assignment.

⇑Table of Contents

Extra Credit

If you want extra credit for this assignment (20 points), configure your Web site to use your own SMTP service of choice and have the destination email sent to BBennett@msjc.edu with a subject of "CSIS 786 Web Email from (your full name)". When you email me your URL please let me know that I need to check my email's inbox for your Web site's email sent from your Contact Web form.