preface

Some time ago, contact with asp.net, with brisket news release system as a practice project, which learned some new knowledge, which has some controls on the verification, summed up here.

The body of the

1. The requiredFieldValidator control (null validation) : Validates that there is no data input in the space. It is a non-null validation control.

Properties:

ControlToValidate: Attribute that must be assigned, representing the ID of the control to be validated

ErrorMessage: the text of the ErrorMessage displayed when authentication fails

Display: The Display of error messages. The value is an enumerated value with three values:

Static, which validates program content as a physical part of the page layout

*None, indicating that the validator content is never displayed inline

DyNamic, which represents the validator content that is dynamically added to the page when validation fails

EnableClientScript: indicates whether client authentication is enabled

In our brisket press release system, we use several of these attributes. For example, when you log in, you don’t enter anything:

Example: Call short

Or a more classic one:

<p> User name: <asp:TextBox ID="txtUserName" runat="server" CssClass="textbox"></asp:TextBox> <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runAT ="server" ErrorMessage=" Please enter a user name!" Text = "*" ControlToValidate = "txtUserName" > < / asp: RequiredFieldValidator > < / p > < p > the secret code: <asp:TextBox ID="txtpassword" runat="server" TextMode="Password" CssClass="textbox"></asp:TextBox> < asp: RequiredFieldValidator ID = "RequiredFieldValidator1" runat = "server" ErrorMessage = "please enter the password" Text = "*" ControlToValidate="txtpassword"></asp:RequiredFieldValidator> </p>Copy the code

2.CompareValidator control: Compares a value in a control with a value in another space or a constant value.

Properties:

ControlToValidate: indicates the ID of the control to be authenticated

CultureInvariantValues: This value indicates whether a value is converted to a culture-neutral format prior to comparison

Operator: The comparison operation to be performed. This value is an enumerated value

2. Equal NotEqual: NotEqual

GreaterThanEqual: greater than; GreaterThanEqual: greater than or equal to

LessThan; LessThanEqual: Less than or equal to

DataTypeCheck: input to the values in the validation controls and BaseCompareValidator. The Type attribute specifies the data Type of the comparison between data types, if unable to convert the value to the specified data Type, the validation fails

Type: The data Type to which the compared value is converted prior to the comparison

RenderUplevel: This value indicates whether the client’s browser supports “up-level” rendering

Text: The Text displayed in the control when validation fails

ValueToCompare: This value is compared to the value in the validation control

Example: For two different passwords entered:
<tr> < TD class="auto-style2" rowSPAN ="2"> Compare validation </ TD > < TD class="auto-style3"> </td> <td class="auto-style1"> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runAT ="server" ControlToCompare="TextBox2" ControlToValidate="TextBox3" ErrorMessage=" Two different passwords!" ForeColor="Red"></asp:CompareValidator> </td> </tr> <tr> < TD class="auto-style3"> </td> <td class="auto-style1"> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </td> </tr>Copy the code

3.RangeValidator control (range validation) : Verifies that the user’s input is within the specified range. It can check the range of number pairs, caption pairs, and date pairs, where the boundaries are represented as constants.

Properties:

MinimumValue: specifies the MinimumValue of the verification range

Example: age is out of range
<tr> < TD class="auto-style2"> Scope validation </ TD > < TD class="auto-style3"> age: </td> <td class="auto-style1"> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <asp:RangeValidator ID="RangeValidator1" RUNAT ="server" ControlToValidate="TextBox4" ErrorMessage=" Your age is not registered, please do not register!" ForeColor="Red" MaximumValue="50" MinimumValue="20"></asp:RangeValidator> </td> </tr>Copy the code

4. RegularExpressionValidator (regular expression) : validation controls whether the value of with a regular expression defined by pattern matching. With this type of validation, you can check for predictable sequences of characters, such as identity card numbers, email addresses, telephone numbers, postal codes, and so on.

what:

Regular expression: a character pattern consisting of ordinary characters and special characters. This pattern describes multiple character strings to be matched when searching for a character body. The regular expression serves as a template to match a character pattern with the searched character string. The characters used in the ValidationExpression property are represented as follows:

“\w” means any single-character match, including underscores;

+ matches the previous character one or more times.

“.” Represents any character;

* indicates that it is easy to combine with other expressions.

[A-z] indicates any uppercase letter.

“\ D” means a number;

“^” indicates the starting position of the matching input.

(In the above expression, quotation marks are not included.)

Example: mailbox is the correct form
<tr> < TD class="auto-style2"> Regular expression validation </ TD > < TD class="auto-style3"> </td> <td class="auto-style1"> <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> <asp:RegularExpressionValidator ID = "RegularExpressionValidator1" runat = "server" ControlToValidate = "TextBox5" ErrorMessage = "please enter the email address right!" ForeColor="Red" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"></asp:RegularExpressionValidator> </td> </tr>Copy the code

5.CustomValidator control (CustomValidator) : custom function validation

Properties:

ClientValidationFunction: Name of the custom client script function to validate

ServarValidate: Occurs when validation is performed on the server

Example: Custom functions
<tr> < TD class="auto-style2"> Custom validation </ TD > < TD class="auto-style3"> Please fill in an even number: </td> <td class="auto-style1"> <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox> <asp:CustomValidator ID="CustomValidator1" RUNAT ="server" ControlToValidate="TextBox6" ErrorMessage=" Please enter an even number!" OnServerValidate ="CumstomValidator" ForeColor="Red"></asp:CustomValidator> </td> </tr>Copy the code

6.ValidationSummary control: Allows error messages for all validation controls on a web page to be summarized in a single location. The function of this control is to display error messages from all validation controls on the phone in a single location.

what:

The Validationsummary control has a number of properties for displaying the ErrorMessage pattern and how the ErrorMessage is displayed that other validation controls do not have, and its display information is specified by the ErrorMessage property of each validation control

DisplayMode: the displayMode of the validation digest, which is an enumeration value

List, the validation summary is displayed in the List

BulletList: Validation summaries are displayed in the bulleted list

SingleParagraph: validation summaries are shown in a SingleParagraph

ShowMessageBox: Whether to display the validation summary in the message box

ShowSummary: Whether to display the validation summary inline

HeaderText: Displays the control title

Example: Validation information is displayed
<tr> <td class="auto-style4"> ValidationSummary </td> <td class="auto-style5" colspan="2"> <asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" ShowMessageBox ="true" /> </td> </tr>Copy the code

Effect:

Afterword.

Note that each Validation control button has a Validation Group property that sets the Validation Group. That is, if there are buttons A and B on A page, button A is required to trigger authentication, but clicking button B does not trigger authentication. Normally, clicking any button triggers validation of the validation control, which is obviously not the case. In this case, set the Validation Group property of the A button to the same value as that of the Validation control. Alternatively, set the CauseValidation property of the B button to False.

Verification control in ASP.net is more basic knowledge, we can often go to review, later practice can also be skilled operation ~