Validation - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Validation

Description:

Post Backs may be prevented until client-side validation succeeds. ... following a Post Back using standard .NET events, properties, and methods ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 25
Provided by: donba5
Category:
Tags: validation

less

Transcript and Presenter's Notes

Title: Validation


1
Validation
  • Validating Control Values
  • Server-side and Client-side Validation

2
What is Validation?
  • It is often important to validate the contents
    entered on a Web form before trying to process
    those contents.
  • Validation may include tasks such as
  • Ensuring a text field is not empty
  • Ensuring that a selection has been made
  • Ensuring that a value is in a certain range
  • Ensuring that the structure of an entry is
    permissible (e.g., a value entered for a social
    security number has the form ddd-dd-dddd).

3
Where Should Validation Occur?
  • Since server-side operations require a post-back
    with its round-trip to the server and back to the
    client, it is more efficient to do some
    validation on the client.
  • For example, if a form contains 10 fields that
    must be completed (non-empty), it is more
    efficient to verify that all 10 contain data
    before permitting a post-back than to do up to 10
    post-backs as each field is skipped.
  • This type of validation requires execution of
    client-side JavaScript or VBScript.

4
Server-side Validation, too
  • It is important to do server-side validation in
    addition to client-side validation because
  • Client browser may not support script execution.
  • Security restrictions on the client may prevent
    script execution even if browser supports it.
  • A malicious user can subvert client-side
    validation, possibly permitting bad things to
    happen on server if it does not also check for
    valid data input.
  • Web Forms support server-side validation through
    numerous validation controls and their ability to
    interact with data entry controls such as
    TextBoxes.

5
Web Forms Validation Controls
  • The following .NET validation controls are
    provided for Web Forms
  • RequiredFieldValidation verify that a required
    input field is not empty
  • RangeValidator verify that a value is in a
    specified range
  • CompareValidator compare value of a control
    with constant or with value of another control
  • CustomValidator user supplied validation
    algorithm
  • RegularExpressionValidator match value of a
    control against a pattern using a regular
    expression
  • ValidationSummary Collection of messages from
    all validation controls on a page

6
Web Forms Validation Controls
  • Validation controls only apply to controls that
    define a single value to validate.
  • Controls such as DataGrids cannot be validated
    using the validation controls.
  • Controls that can be validated
  • HTMLInputText
  • HTMLTextArea
  • HTMLSelect
  • HTMLInputFile
  • TextBox
  • DropDownList
  • ListBox
  • RadioButtonList

7
Web Forms Validation Controls
  • Web Forms Validation Controls provide both of the
    following
  • Client-side validation through JavaScript if
    the client browser supports JavaScript and if the
    clients security permits script execution. Post
    Backs may be prevented until client-side
    validation succeeds.
  • Server-side validation following a Post Back
    using standard .NET events, properties, and
    methods

8
Common Ideas in Validation
  • Often, a message, symbol, or other output is
    placed next to a control with invalid data.
  • May be a contrasting color to call visual
    attention to it
  • If symbol, messages may appear in a MessageBox or
    in a summary list elsewhere on the page
  • Often multiple types of validation are needed for
    a single input control.
  • For example, a required numeric field needs to be
    evaluated for being non-empty and for having a
    value in the proper range using different
    validation controls.
  • Different error messages are used to accompany
    the different types of errors.

9
Using a Validation Control
  • Commonly, place a validation control on a form
  • Next to the control it is to validate
  • Where you want the error message or error
    indicator to be displayed
  • Validation control typically has a
  • ControlToValidate property identifying which
    control it validates
  • ErrorMessage text to display or to place in the
    error summary
  • Initial value (may be empty, indicating no
    display if valid data is entered).
  • Text Text to display in place, if the
    ErrorMessage is displayed in Summary.

10
Validation Control Display Property
  • Indicates whether the control occupies space on
    the visible form when no error has been detected
  • Static always occupies space even if hidden
  • Dynamic occupies space if error useful if
    multiple validation types (e.g., required and
    range) are needed so that only the one
    appropriate message will occupy the space next to
    control with the invalid value.
  • None never occupies space
  • used if the message is only to appear in Summary,
    for example

11
RequiredFieldValidation Control
  • Used to determine whether the associated control
    has no value. For example
  • Empty TextBox
  • Nothing selected in ListBox
  • This is especially important because all other
    controls validate an empty field (or unselected
    item) as being valid
  • Often used with other validation controls
  • Used to prevent a post-back until all required
    fields are filled/selected

12
RangeValidator Control
  • Used to verify a value is in proper range.
  • Properties include
  • Type of data
  • MinimumValue, MaximumValue
  • ControlToValidate
  • ErrorMessage message to display
  • Text Symbol/text to use where RangeValidator
    control is when ErrorMessage is placed in Summary
  • Tooltip

13
Data Types Supported
  • Data types supported by RangeValidator and
    CompareValidator
  • String
  • Integer
  • Double
  • Date
  • Currency - Specifies a monetary data type

14
CompareValidator
  • Compare one controls value with another
    controls value or with a constant.
  • Properties include
  • Data Type
  • ControlToCompare
  • ValueToCompare
  • ControlToValidate
  • ErrorMessage
  • Text
  • ValidationCompareOperator

15
ValidationCompareOperator
  • Equal
  • NotEqual
  • GreaterThan
  • GreaterThanEqual
  • LessThan
  • LessThanEqual
  • DataTypeCheck
  • A data type comparison of the value entered into
    the input control being validated and the data
    type specified by the BaseCompareValidator.Type
    property.
  • Validation fails if the value cannot be converted
    to the specified data type.
  • Note   The ControlToCompare and ValueToCompare
    properties are ignored when this operator is used.

16
RegularExpressionValidator
  • Used to verify that the form of input data is
    correct by using pattern matching.
  • Can verify that a telephone number, social
    security number, e-mail address, URL, or similar
    item is formatted correctly and could be valid.
  • It does not guarantee that the input is valid
    only that its format is valid.

17
RegularExpressionValidator
  • Some important properties of a RegularExpressionVa
    lidator control
  • ControlToValidate
  • ToolTip
  • ErrorMessage
  • Text
  • ValidationExpression a regular expression
    describing the pattern to be used as the input
    value is checked for a match with the pattern

18
RegularExpressionValidator
  • Some common items are provided and can be
    selected from a combo box
  • Other resources for regular expressions include
  • Regulator at http//regex.osherove.com/
  • Expresso at http//www.ultrapico.com/Expresso.htm

19
Some Regular Expression Characters
20
CustomValidator
  • If you wish to provide your own validation
    algorithm, use a CustomValidator.
  • In this case, you should provide both the
    JavaScript client-side algorithm and the .NET
    server-side event handler.
  • Details not covered here.

21
ValidationSummary Control
  • This control can be used to display all error
    messages gathered from other validation controls
    that failed validation.
  • Can be displayed in list, bulleted list, or
    paragraph format.
  • Alternatively, can be displayed using
    MessageBox-like window.

22
ValidationSummary Control
 
 
ValidationSummary Control showing MessageBox
Text property of RequiredFieldValidator
ValidationSummary Control Contents are the
ErrorMessages from the individual controls
23
IsValid Property
  • The base class for validation controls has a
    property named IsValid. It is set to false if
    the associated control does not pass validation.
  • The Page class contains an IsValid property that
    is set to false if any validation controls
    IsValid property is false.
  • On server-side, if a control fails to pass
    validation, the ONLY THING that happens is
    IsValid is set to false.
  • Remember, if client-side validation is performed,
    the post-back is blocked until validation passes.
  • If client-side validation is not performed, this
    becomes important.
  • Remaining code is executed normally. To prevent
    unwanted actions, check the IsValid property in
    subsequent event handler(s).
  • If Me.IsValid Then
  • Take some action
  • End If

24
Validate Method
  • Each validation control has a Validate method
    that performs the designated validation.
  • Each Page has a Validate method that cycles
    through all validation controls on the Page and
    invokes their Validate methods.
  • Page.Validate() is normally invoked AFTER
    Page_Load but before other user-related events
    such as a button click event.
  • IsValid is only meaningful after Validate is
    called.
  • One may call Validate manually for any of the
    validation controls individually or for the
    entire Page if necessary.
Write a Comment
User Comments (0)
About PowerShow.com