0% found this document useful (0 votes)
410 views

Tutorial 7 (Answer)

The document discusses various validation controls in ASP.NET such as RequiredFieldValidator, RangeValidator, CompareValidator, CustomValidator, and RegularExpressionValidator. It explains the importance of using validation controls to ensure valid data and reduce incorrect submissions. While client-side validation is more efficient, server-side validation is more reliable since it does not depend on client-side scripts. Both methods can be used by setting the validation controls' properties. RegularExpressionValidator checks patterns against regular expressions while CustomValidator allows custom validation logic.

Uploaded by

miChiKoLee
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
410 views

Tutorial 7 (Answer)

The document discusses various validation controls in ASP.NET such as RequiredFieldValidator, RangeValidator, CompareValidator, CustomValidator, and RegularExpressionValidator. It explains the importance of using validation controls to ensure valid data and reduce incorrect submissions. While client-side validation is more efficient, server-side validation is more reliable since it does not depend on client-side scripts. Both methods can be used by setting the validation controls' properties. RegularExpressionValidator checks patterns against regular expressions while CustomValidator allows custom validation logic.

Uploaded by

miChiKoLee
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

AACS4134 Internet Programming

Tutorial 7 (Solution)
1. Explain the types of validation controls available. RequiredFieldValidator RangeValidator CompareValidator CustomValidator RegularExpressionValidator ValidationSummary

2. (i)

Discuss the importance of using validation controls within a Web form. Validation controls are used to ensure that data entered into a form conforms to some specific criteria (e.g. data type set in database), and to reduce the chances of random gibberish being sent back to the server. Also, the web applications are collecting information from a user, you want to ensure that the data that you collect is valid. Some users are not interested in spending enough time to enter the correct information into a form, and in some cases, users might even intentionally enter false information to gain access or get past a certain step in your application's workflow process. (ii) What are the drawbacks if we do not enable client-side validation but only server-side validation? The bad thing about server-side validation is that it requires trips back and forth to the server. This takes a lot of resources and makes for a slower-paced form for the user. Nothing is more annoying to a user who is on a dial-up connection than clicking the Submit button on the form and then waiting for 20 seconds to find out that they didn't enter their password correctly. Second drawback is when you used server-side validation, if something the user entered was wrong, you could repost the form and ask the user to correct the information in that particular field of the form. Sometimes, you carried the correct input from the other fields back to the form page, and populated the fields for the users so they didn't have to reenter the same information again. Some sites on the Internet don't carry this inputted information back to the form page, and the user is then required to enter all the information into the form a second time. Obviously, this may cause people to leave your site for another. (source from MSDN: http://msdn.microsoft.com/en-us/library/aa479013.aspx)

(iii)

You can validate the controls in a form by using two methods of coding: serverside validation and client-side validation. Justify which method is better.

Students preferences may vary, as long as with acceptable justification. Client-side validation: Advantages: takes care of the problem of making unnecessary trips to the server, It was most efficient to do the validation of the form on the client-side to limit the number of requests and responses required to work through an application. Disadvantage: It requires another language (JavaScript or VBScript) to learn and manage.

Chapter 7 Validation Controls

AACS4134 Internet Programming

There are always problems getting your JavaScript/VBScript code to work on different browsers. Server-side validation: Advantage:

you were never quite sure if the requesting browser would understand the scripting code that you used for the validation. So, it was usually better, especially for critical Web applications, to bring the validation to the server. Disadvantage: problem of making unnecessary trips to the server Conclusion: Use ASP.NET validation controls the best, can use all validation controls at server side as well as client side, you just need to set true for client side validation in its property.

3. Differentiate between RegularExpressionValidator and CustomValidator.


Provide examples to support your answers.
RegularExpressionValidator Checks that the user's entry matches a pattern defined by a regular expression. This is a good control to use to check e-mail addresses and phone numbers Checks the user's entry using custom-coded validation logic. The CustomValidator server control enables you to develop your own custom server-side or client-side validations. At times, you may want to compare the user's input to a value in a database, or to determine whether his input conforms to some arithmetic validation that you are looking for (for instance, if the number is even or odd). You can do all this and more by using this type of validation control.

CustomValidator

Students MAY OR MAY NOT provide examples of CODES, as long as they present the ideas when to use RegularExpressionValidator and when to use CustomValidator. RegularExpressionValidator sample code:
Email: <asp:TextBox id="TextBox1" runat="server"></asp:TextBox> &nbsp; <asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="You must enter an email address" ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"> </asp:RegularExpressionValidator>

CustomValidator sample code 1 by mixing Client-side and Server-side validations:


<%@ Page Language="C#" %> <script runat="server"> void Button1_Click(Object sender, EventArgs e) {

Chapter 7 Validation Controls

AACS4134 Internet Programming Label1.Text = "VALID NUMBER!"; } </script> <html> <head> <script language="JavaScript"> function validateNumber(oSrc, args) { args.IsValid = (args.Value % 5 == 0); } </script> </head> <body> <form runat="server"> <p> Number: <asp:TextBox id="TextBox1" runat="server"></asp:TextBox> &nbsp; <asp:CustomValidator id="CustomValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Number must be divisible by 5" ClientValidationFunction="validateNumber"> </asp:CustomValidator> </p> <p> <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button> </p> <p> <asp:Label id="Label1" runat="server"></asp:Label> </p> </form> </body> </html>

CustomValidator Sample code 2 by validating drop-downlist:


<%@ Page Language="C#" %> <script runat="server"> void CustomValidator1_ServerValidate(Object source, ServerValidateEventArgs args) {

Chapter 7 Validation Controls

AACS4134 Internet Programming

args.IsValid = (CheckBox1.Checked == true); } void Button1_Click(Object sender, EventArgs e) { if (Page.IsValid) { Label1.Text = "Thank you for your donation!"; } else { Label1.Text = ""; } } </script> <html> <head> </head> <body> <form runat="server"> <p> Check checkbox if you want to donate $10 </p> <p> <asp:CheckBox id="CheckBox1" runat="server" Text="Donate $10"></asp:CheckBox> &nbsp; <asp:CustomValidator id="CustomValidator1" runat="server" ErrorMessage="Please donate $10" OnServerValidate="CustomValidator1_ServerValidate"> </asp:CustomValidator> </p> <p> <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button> </p> <p> <asp:Label id="Label1" runat="server"></asp:Label> </p> </form> </body>

Source: (source from MSDN: http://msdn.microsoft.com/en-us/library/aa479013.aspx)

Chapter 7 Validation Controls

AACS4134 Internet Programming

4. (i)

Your task is to explain to him what kind of validation controls that he can use to achieve his aim and why they are appropriate in the Web form that he has built. The student should identify the validation controls that are available. The student should explain which validation controls should be applied for each of the criteria.

The student should explain why the validation controls suggested are appropriate. Sample of answers: 4 RequiredFieldValidators are required for all the textboxes to check the textbox contains value. RegularExpressionValidator is used for password textbox. RangeValidator is applied to age textbox. CompareValidator is applied to date of birth textbox. ii)

For each of the different types of validation controls that you have suggested in Q4(i), identify which properties of the validation controls that should be changed to achieve the following criteria: a. Password length must be 6 characters

ValidationExpression="\w{6}"
b. Age range is between 1 to 99

Type = Integer MinimumValue="1" MaximumValue="99"


c. Date of birth must be a valid date format

Type="Date" Operator="DataTypeCheck" For reference: RegularExpressionValidator Example <asp:RegularExpressionValidator id="validatePasswordLength" runat="server" ErrorMessage="Password must be 6 characters" ControlToValidate="txtPassword" ValidationExpression="\w{6}"> </asp:RegularExpressionValidator> RangeValidator Example <asp:RangeValidator id="validateAgeRange" runat="server" ControlToValidate="txtAge" ErrorMessage="Please enter a valid age" MinimumValue="1" MaximumValue="99" Type="Integer"></asp:RangeValidator> CompareValidator Example <asp:CompareValidator id="validateDateFormat" runat="server" ControlToValidate="txtDOB" ErrorMessage="Please enter correct date format" Type="Date" Operator="DataTypeCheck"> </asp:CompareValidator>
Chapter 7 Validation Controls

AACS4134 Internet Programming

Chapter 7 Validation Controls

You might also like