Asp. NET hands-on Tutorials (6)

Source: Internet
Author: User
Tags format definition contains expression key net range valid

Form validation server Control

Authentication Introduction

The network formation framework includes an easy-to-use and powerful server control for validation that checks for errors in the input form and displays the prompts to the user when necessary. Add validation controls to the design trap table just as you would add other server controls. Validation controls have different validation types, such as range checking or pattern matching, and adding a RequiredFieldValidator control ensures that users cannot skip a field that requires input. You can combine several validation controls on an input control. For example, you might want a field that cannot be empty, and you must enter a range of values. Validation controls work in conjunction with a limited subset of server controls. That is, for different controls, you may need to validate the values of different properties.

Validating control types

The simplest validation form requires an input field. Whether the user has entered any value in the field, it is validated. If all the fields in the page are validated through, then the page is also validated (the page is valid). The following example illustrates the use of the RequiredFieldValidator validation control.

Specific validation conditions require different validation controls, such as range checking or pattern matching.

Client-side validation

In general, validation checks are always performed on the server side of the authentication control. However, if the user is working with a DHTML-enabled browser, the validation controls can also be validated using client-side scripting. With client confirmation, any detected error from the client can be found when the form is submitted to the server. When any one of the validation controls discovers an error, the form submitted to the server is canceled and the text property between the validation controls is displayed. This allows the user to correct the input errors before submitting the form to the server. When the field that contains the error loses focus, the field is immediately validated. This provides a rich, interactive validation process for the user.

Note: The Web Forms page framework always performs validation on the server side, even if validation is already performed on the client. This helps prevent impersonation and subscription transactions from being validated.

Client-side validation is allowed by default. If the client condition allows, the high version browser authentication mode will be performed automatically. To prevent client authentication, set the page's ClientTarget property to "downlevel" ("uplevel" Force client-side validation)

Display validation error messages

When the user's input starts processing (for example, when the form has been submitted), the Web Forms page framework passes the user's input to the associated validation control. The validation control tests the user's input and sets the property to flag whether the input passes the validation test. The IsValid property of the page is not set until all the validation controls have finished running. If any of the validation controls show a validation failure, the entire page validation is also invalid.

If the validation control monitors the error, the control or the ValidationSummary control on the page displays an error message on the page. The ValidationSummary control displays the error prompt if the page isvalid is equal to false. (that is, any one of the validation controls will activate ValidationSummary), it can monitor validation errors for each validation control, and all errors are displayed all at once. The following example shows how the ValidationSummary control can be incorrectly prompted.

Using the Compare validation control CompareValidator

The CompareValidator server validation control is used to compare the values of two controls. It uses three key properties to validate it. ControlToValidate and ControlToCompare contain the values to compare, Operator defines the type of comparison to be performed---for example, equal to or not equal to, and so on. CompareValidator performs validation by evaluation of an expression, as follows:

(ControlToValidate ControlToCompare)

If the value of the expression is true, the validation result is passed.

The following example shows how to use the CompareValidator control

Using the scope validation control RangeValidator

The RangeValidator server control tests whether the input value is within a given range. RangeValidator uses three key properties to perform validation. ControlToValidate contains validated objects, MinimumValue and MaximumValue define the minimum and maximum values for a valid range.

The following example shows how to use the RangeValidator control.

validating controls using regular expressions RegularExpressionValidator

The RegularExpressionValidator server control is used to check whether user input matches the pattern defined by the regular expression. This control allows you to check the predictable string series, such as Social Security numbers, e-mail addresses, phone numbers, postal codes, and so on.

RegularExpressionValidator uses two key properties to perform validation. ControlToValidate contains the control object to validate, ValidationExpression contains the regular expression to match.

The original textbook is less about regular expressions, here I would add:

A regular expression is a powerful string definition rule, such as "*" and "familiar" under DOS. "He was first introduced into UNIX. In ASP.net, his format is [acceptable character]{range}. For example, [a-z]{3,7} represents a minimum of three characters, containing up to 7 characters, where the character can only be a range of ' a ' ~ ' Z ', such as "Krpam" is legal and "12fe3" or "5W" is illegal.

in [] In addition to the definition of?-?, you can write the acceptable characters directly, for example [ourasp] means to accept only the characters ' o ', ' u ', ' R ', ' a ', ' s ', ' P ', and ^ to represent an inverse set, for example [^aurasp], which means that except ' o ', ' u ', ' R ', Any character other than ' a ', ' s ', ' P ' is accepted. If any character is acceptable, use the "." to indicate. For example. {0,} indicates that any number of arbitrary characters can be accepted.

In {}, in addition to the commonly used format such as {3}, {2-13}, there are also the following commonly used formats: {N,} to enter at least n characters.

The "|" symbol represents or (or), for example, [a-za-z]{3}| [0.9] {3} indicates that the acceptable character is 3 English letters or 3 numeric characters. Please note that he differs from [a-za-z0-9]{3}. For the convenience of reading, we usually put the "|" The string of symbols is placed in (), for example ([a-za-z]{3}|[ 0.9]{3}) above []{}. () | are special symbols in regular expressions. If you want to include these characters in an acceptable string, you must precede them with a slash \. For example, "\. {3} ' indicates that 3 '. ' must be entered.

The following example shows how to use the ValidationExpression control.

Using custom validation Controls custom Validation

The CustomValidator server control invokes a user-defined function to perform a standard validation control that cannot be processed with validation. The custom function can be executed either on the server side or in client script (such as VBScript or JScript). For client-side custom validation, the custom function name must be specified in the ClientValidationFunction property. The custom function must use this parameter format:

function Myvalidator (source, arguments)

Note The source parameter is the client CustomValidator, the arguments parameter is an object with two property value and IsValid, the Value Property object, and the IsValid property is a Boolean value that sets the result of the validation returned.

For server-side custom validation, place your custom validation in the onservervalidate of the validation control.

The following example shows how to use the CustomValidator control.

Example of using all validation controls

The following example shows a typical registration form that uses all of the validation controls discussed in this chapter

Summary of this chapter

1, validation controls can be used to verify any input to the Web Forms page.

2, for a given input field, you can use several validation controls to verify.

3. In addition to server-side validation, you can use client authentication to improve the usability of the form.

4, CustomValidator controls allow users to customize validation rules.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.