Presentation is loading. Please wait.

Presentation is loading. Please wait.

Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation.

Similar presentations


Presentation on theme: "Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation."— Presentation transcript:

1 Session 6: Validating User Input

2 Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation Controls Input Validation Controls RequiredFieldValidator CompareValidator RangeValidator Other Validator Controls Page Validation

3 What Is Input Validation? Verifies that a control value is correctly entered by the user Blocks the processing of a page until all controls are valid Avoids spoofing or the addition of malicious code

4 Client-Side and Server-Side Validation ASP.NET can create both client-side and server-side validation Client-side validation Dependent on browser version Instant feedback Reduces postback cycles Server-side validation Repeats all client-side validation Can validate against stored data Valid? User Enters Data No Yes Error Message Client Server Web Application Processed

5 ASP.NET Validation Controls ASP.NET provides validation controls to: Compare values Compare to a custom formula Compare to a range Compare to a regular expression pattern Require user input Summarize the validation controls on a page

6 Adding Validation Controls to a Web Form 1.Add a validation control 2.Select the input control to validate 3.Set validation properties <asp:Type_of_Validator id="Validator_id" runat="server" ControlToValidate="txtName" ErrorMessage="Message_for_error_summary" Display="static|dynamic|none" Text="Text_to_display_by_input_control"> <asp:Type_of_Validator id="Validator_id" runat="server" ControlToValidate="txtName" ErrorMessage="Message_for_error_summary" Display="static|dynamic|none" Text="Text_to_display_by_input_control"> 11 22 33

7 Positioning Validation Controls on a Web Form Create error messages Select display mode Static Dynamic

8 Combining Validation Controls Can have multiple validation controls on a single input control Only the RequiredFieldValidator checks empty controls

9 Input Validation Controls RequiredFieldValidator CompareValidator RangeValidator

10 The RequiredFieldValidator Control The RequiredFieldValidator is used to ensure that a required field is filled in by the user ControlToValidate (ID of field to validate) ErrorMessage (if ValidatorSummary is used) Text (error message to display) InitialValue (prompt visible in field)

11 The CompareValidator Control The CompareValidator is used to compare the value entered with (an)other value(s) ControltoValidate (ID of field to validate) ValueToCompare (value being compared) or ControlToCompare (ID of field being compared) Type (specifies data type to compare to) Operator (specifies type of comparison)

12 The RangeValidator Control RangeValidator ControlToValidate (ID of field to validate) MinimumValue (lower limit of range) MaximumValue (upper limit of range) Type (data type of field)

13 The RegularExpressionValidator Control Used when input must conform to a pre-defined pattern Visual Studio.NET includes patterns for: Telephone numbers Postal codes E-mail addresses <asp:RegularExpressionValidator … ControlToValidate="US_PhoneNumber"… ValidationExpression=" ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4} " …>* <asp:RegularExpressionValidator … ControlToValidate="US_PhoneNumber"… ValidationExpression=" ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4} " …>*

14 The CustomValidator Control Can validate on client-side, server-side, or both ClientValidationFunction OnServerValidate Validate with: Formula Data COM objects Web Service

15 The Page.IsValid Property Polls all validation controls Sub cmdSubmit_Click(s As Object, e As EventArgs) If Page.IsValid Then Message.Text = "Page is valid!" ' Perform database updates or other logic here End If End Sub Sub cmdSubmit_Click(s As Object, e As EventArgs) If Page.IsValid Then Message.Text = "Page is valid!" ' Perform database updates or other logic here End If End Sub

16 Using the ValidationSummary Control Collects error messages from all validation controls on the page Can display text and error messages Use Text="*" to indicate the location of the error <asp:ValidationSummary id="valSummary" runat="server" HeaderText="These errors were found:" ShowSummary="True" DisplayMode="List"/> <asp:ValidationSummary id="valSummary" runat="server" HeaderText="These errors were found:" ShowSummary="True" DisplayMode="List"/>


Download ppt "Session 6: Validating User Input. Outline Overview of User Input Validation Client-Side and Server-Side Validation ASP.NET Validation Controls Using Validation."

Similar presentations


Ads by Google