1 / 16

Session 6: Validating User Input

Session 6: Validating User Input. 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

clara
Download Presentation

Session 6: Validating User Input

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 spoofingor the addition ofmalicious code

  4. Client-Side and Server-Side Validation User Enters Data • 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 Error Message Valid? No Client Yes Server Valid? No Yes Web ApplicationProcessed

  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 • Select the input control to validate • Set validation properties 2 3 <asp:TextBox id="txtName" runat="server" /> <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>

  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 >

  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

  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"/>

More Related