Export (0) Print
Expand All

ValidationHelper Class

Implements validation for user input.

System.Object
  System.Web.WebPages.ValidationHelper

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

public sealed class ValidationHelper

The ValidationHelper type exposes the following members.

  NameDescription
Public propertyFormFieldGets 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 memberInvalidCssClassGets 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 memberValidCssClassGets 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

  NameDescription
Public methodAdd(IEnumerable<String>, IValidator[])Registers a list of user input elements for validation.
Public methodAdd(String, IValidator[])Registers a user input element for validation.
Public methodAddFormErrorAdds an error message.
Public methodClassForRenders an attribute that references the CSS style definition to use when validation messages for the user input element are rendered.
Public methodEquals (Inherited from Object.)
Public methodForRenders attributes that enable client-side validation for an individual user input element.
Public methodGetErrorsReturns a list of current validation errors, and optionally lets you specify a list of fields to check.
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodIsValidDetermines whether the contents of the user input fields pass validation checks, and optionally lets you specify a list of fields to check.
Public methodRequireField(String)Registers the specified field as one that requires user entry.
Public methodRequireField(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 methodRequireFieldsRegisters the specified fields as ones that require user entry.
Public methodToString (Inherited from Object.)
Public methodValidatePerforms validation on elements registered for validation, and optionally lets you specify a list of fields to check.
Top

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.

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