Asp.net| Tutorial Form validation server control
Authentication Introduction
The network formation framework includes an easy-to-use and powerful server control for validation to check for errors in input forms
Error, and when necessary, display the message to the user. Add a validation control to the design trap table, just like Tim
Add the same as other server controls. Validation controls have different validation types, such as range checking or pattern matching, adding
A RequiredFieldValidator control can guarantee that a user cannot skip a field that requires input.
You can combine several validation controls on an input control. For example, you may want a field that cannot be empty, and
You must enter a value for a range. Validation controls work in conjunction with a limited subset of server controls. In other words, for
Different controls, you may need to validate the values of different properties. The following table lists the property controls for the input controls that may be validated
Validating properties
HtmlInputText Value
HtmlTextArea Value
HtmlSelect Value
HtmlInputFile Value
TextBox Text
ListBox Selecteditem.value
DropDownList Selecteditem.value
RadioButtonList Selecteditem.value
Validating control types
The simplest validation form requires an input field. Regardless of whether the user has entered any value in the field, it is validated by the
。 If all the fields in the page are validated through, then the page is also validated (the page is valid). Below
Example illustrates the use of the RequiredFieldValidator validation control.
C # validator1.aspx
[Run] | [Source code]
Specific validation conditions require different validation controls, such as range checking or pattern matching. The following table lists all the validation
Control: Control Name Description
RequiredFieldValidator guarantee that the user cannot ignore input.
CompareValidator compares user input and constants with comparison operators (less than, equal to, greater than, and so on) (or
Property value of another control)
RangeValidator checks the user's input within a specific maximum and minimum range. You can use a pair of numbers.
Word, character, or date to validate the input range. A range boundary can use an expression that can get a constant.
RegularExpressionValidator checks that user input matches a pattern defined by a regular expression. This validation
Allows you to check for predictable sequences of characters, such as Social security number, e-mail address, phone number, postal code
Wait a minute.
CustomValidator uses the validation logic that you write to check the user's input. This validation allows you to check the running
Time to get the value.
ValidationSummary Summary Displays validation error messages for all validation controls on the page
Client-side validation
In general, validation checks are always performed on the server side of the authentication control. However, if the user is using a support
DHTML browsers work, and validation controls can also be validated using client-side scripting. With client confirmation, when the table Tanti
When handed to the server, any error detected by the client can be found. When any one of the validation controls finds an error
, the form that is submitted to the server is canceled, and the text property between the validation controls is displayed. This allows the user to
Correct the input error before submitting the form to the server. When the field that contains the error loses focus, the field horse
will be validated again. 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 ClientTarget property of the page to "downlevel" ("uplevel" Force guest
User-side validation)
C # validator2.aspx
[Run] | [Source code]
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
Try. The IsValid property of the page is not set until all the validation controls have finished running. If any one of the validation controls is explicitly
Shows the validation failure, and the entire page validation is also invalid.
If the validation control monitors the error, the control or the ValidationSummary control on the page will display on the page
Indicates an error message. The ValidationSummary control displays the error prompt if the page isvalid equals
False (that is, any validation control that does not pass will activate ValidationSummary), and it can
To monitor validation errors for each validation control and to display all errors at once. The following example
Demonstrates how the ValidationSummary control can be incorrectly prompted.
C # validator3.aspx
[Run] | [Source code]
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 do it
The validation. ControlToValidate and ControlToCompare contain the values to compare, Operator defines the
The type of comparison performed---for example, equal to or not equal to, and so on. CompareValidator by evaluation of an expression
Row validation, just like the following:
(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
C # validator4.aspx
[Run] | [Source code]
Using the scope validation control RangeValidator
The RangeValidator server control tests whether the input value is within a given range. RangeValidator use
Three key properties to perform validation. ControlToValidate contains validated objects, MinimumValue and Max
Imumvalue defines the minimum and maximum values for a valid range.
The following example shows how to use the RangeValidator control.
C # validator5.aspx
[Run] | [Source code]
validating controls using regular expressions RegularExpressionValidator
The RegularExpressionValidator server control is used to check whether user input is defined with a regular expression
The. This control allows you to check the predictable string series, such as Social Security numbers, e-mail addresses, electricity
Phone number and zip code, and so on.
RegularExpressionValidator uses two key properties to perform validation. ControlToValidate contains to
The validated control object that contains the regular expression used to match the validationexpression.
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 the first
Introduced in Unix. In ASP.net, his format is [acceptable character]{range}. For example, [a-z]{3,7} table
Shows at least three characters, with a maximum of 7 characters, where the character can only be a range of ' a ' ~ ' Z ', such as "Krpa
M "is legal, and" 12fe3 "or" 5W "is illegal.
in [] In addition to the definition of?-?, you can write an acceptable character directly, for example, [ourasp] means to accept only '
o ', ' u ', ' R ', ' a ', ' s ', ' P ', and ^ to represent the reverse collection, for example [^aurasp]
, indicating that any character is acceptable except for ' o ', ' u ', ' R ', ' a ', ' s ', ' P '. If you indicate
can accept any character, using the "." to indicate. For example. {0,} indicates that any number of arbitrary characters can be accepted.
In {}, in addition to the commonly used format of {3}, {2-13}, there are the following commonly used formats: {N,}
Enter less than 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 digit characters. Please note that he differs from [a-za-z0-9]{3}. For the convenience of reading, we will usually put the containing
| The string of symbols is placed in (), for example ([a-za-z]{3}|[ 0.9]{3})
The []{} above. () | are special symbols in regular expressions. If you want to include these characters in the acceptable string
, you must precede these characters with a slash \. For example, "\. {3} ' indicates that 3 '. ' must be entered.
The following example shows how to use the ValidationExpression control.
C # validator6.aspx
[Run] | [Source code]
C # validator7.aspx
[Run] | [Source code]
Using custom validation Controls custom Validation
CustomValidator server control invokes a user-defined function to perform a standard validation control cannot be processed
Certificate 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
Do point out. The custom function must use this parameter format:
function Myvalidator (source, arguments)
Note that the source parameter is client CustomValidator, and the arguments parameter is with two property value and
IsValid object, Value Property object, IsValid property is a Boolean value that is used to set the return test
Certificate results.
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.
C # validator8.aspx
[Run] | [Source code]
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
C # validator9.aspx
[Run] | [Source code]
Summary of this chapter
1. Validation controls can be used to validate any input to the Web Forms page.
2. For a given input field, you can verify it by using several validation controls.
3. In addition to server-side validation, you can use client authentication to improve the availability of the form.
4. The CustomValidator control allows users to customize validation rules.
In the following chapters, we start talking about user-defined controls
To be continued =========asp.net Chinese professional Network (ourasp.net) ==========wincheer
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.