ValidationHelper Class

Implements validation for user input.

Inheritance Hierarchy

System.Object
  System.Web.WebPages.ValidationHelper

Namespace:  System.Web.WebPages
Assembly:  System.Web.WebPages (in System.Web.WebPages.dll)

Syntax

'Declaration
Public NotInheritable Class ValidationHelper
'Usage
Dim instance As ValidationHelper
public sealed class ValidationHelper
public ref class ValidationHelper sealed
[<SealedAttribute>]
type ValidationHelper =  class end
public final class ValidationHelper

The ValidationHelper type exposes the following members.

Properties

  Name Description
Public property FormField Gets the name of the current form. This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Public propertyStatic member InvalidCssClass Gets the name of the class that is used to specify the appearance of error-message display when errors have occurred. This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
Public propertyStatic member ValidCssClass Gets the name of the class that is used to specify the appearance of error-message display when errors have occurred. This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Top

Methods

  Name Description
Public method Add(IEnumerable<String>, array<IValidator[]) Registers a list of user input elements for validation.
Public method Add(String, array<IValidator[]) Registers a user input element for validation.
Public method AddFormError Adds an error message.
Public method ClassFor Renders an attribute that references the CSS style definition to use when validation messages for the user input element are rendered.
Public method Equals (Inherited from Object.)
Public method For Renders attributes that enable client-side validation for an individual user input element.
Public method GetErrors Returns a list of current validation errors, and optionally lets you specify a list of fields to check.
Public method GetHashCode (Inherited from Object.)
Public method GetType (Inherited from Object.)
Public method IsValid Determines whether the contents of the user input fields pass validation checks, and optionally lets you specify a list of fields to check.
Public method RequireField(String) Registers the specified field as one that requires user entry.
Public method RequireField(String, String) Registers the specified field as one that requires user entry and registers the specified string as the error message to display if no value is supplied.
Public method RequireFields Registers the specified fields as ones that require user entry.
Public method ToString (Inherited from Object.)
Public method Validate Performs validation on elements registered for validation, and optionally lets you specify a list of fields to check.

Top

Remarks

The Validation helper lets you register user input elements (for example, <input> elements) and define validation for them. For example, you can use the helper to register that a user input element named "description" is required and that the maximum length of the entry in the field is 15 characters. To register an element for validation, you call Add, RequireField, or RequireFields.

In addition to registering a user input element for validation, you typically add ValidationSummary or ValidationMessage calls in the markup of the page. These methods display validation error messages.

When the page runs, the Validation helper checks user input automatically. If the input does not pass validation, the helper displays the errors at the location in the page where you have put the ValidationSummary or ValidationMessage calls. In code, you can call IsValid to determine whether all validation checks have passed.

The Validation helper always performs validation checks in server code after a page a page has been submitted. You can also add client-side validation checking. To do that, you add JavaScript libraries to the page and call the For(String) and ClassFor(String) methods. These methods dynamically render attributes that can be hooked by JavaScript routines in order to perform validation in the browser.

NoteNote

Even if validation is performed in the client, it is performed again in server code so that the user cannot bypass validation by disabling client script.

If you use ValidationSummary or ValidationMessage to display validation error messages, you can specify the appearance of the errors by creating CSS classes. The names of the CSS classes that are used are predefined. To specify the look of the user input element or of the validation error message that's associated with the element, define the following CSS style classes:

  • input-validation-error. This class is used to style the input element if a validation error has occurred.

  • input-validation-valid. This class is used to style the input element if no validation error has occurred. In general, you do not have to define this class, since the default state of the user input element is that there is no error.

  • field-validation-error. This class is used to style the output of the ValidationMessage method if there is a validation error in the associated user input element.

  • field-validation-valid. This class is used to style the output of the ValidationMessage method when there is no a validation error in the associated user input element. This class is useful if you want to define a fixed layout for the validation error message so that it takes up the same amount of space whether an error has occurred or not.

  • validation-summary-errors. This class is used to style the output of the ValidationSummary method if there is a validation error in the associated user input element.

  • validation-summary-valid. This class is used to style the output of the ValidationSummary method when there is no a validation error in the associated user input element. This class is useful if you want to define a fixed layout for the validation error message so that it takes up the same amount of space whether an error has occurred or not.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Web.WebPages Namespace