C Verifies Simple Use of Control validator

  • 2021-06-29 11:50:00
  • OfStack

ASP.NET provides a simple and practical set of server controls for developers to verify that the information entered by users is valid.The main properties of these controls are id (control's only 1id), ControlToValidate (validated control's id), ErrorMessage (text displayed in the control when validation fails), and runat (which specifies that the control is a server control).Must be set to "server".

1, RequiredFieldValidator: Verify a required field, and if it is not filled in, you will not be able to submit information.

The following example validates whether the input to the text box is empty, and an error occurs when the input is empty.The code is as follows:


<ASP:TextBox id="txtName" RunAt="Server"/>
<ASP:RequiredFieldValidator id=" RequiredFieldValidator1" Runat="Server"   ControlToValidate="txtName"   ErrorMessage=" User name cannot be empty "  ForeColor="red">*</ASP:RequiredFieldValidator>

2. CompareValidator: Compare verification.Compare two fields with equal values, such as Password and Confirm Password.Compare a field with a specific value.

The following example is input password validation for two text boxes, which fails if both text boxes do not enter content 1.The code is as follows:


<asp:TextBox ID="txtPWD1" runat="server" TextMode="Password"></asp:TextBox>
<asp:TextBox ID="txtPWD2" runat="server" TextMode="Password"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" ForeColor="Red" runat="server" ErrorMessage=" Twice Password Entry No 1 To " ControlToValidate="txtPWD1" ControlToCompare="txtPWD2"    type="String"></asp:CompareValidator>

The following example validates the input value for a text box and errors when the input is equal to a value.The code is as follows:


<ASP:TextBox id="txtName" RunAt="Server"/>
<ASP:CompareValidator id=" CompareValidator1" Runat="Server"   ControlToValidate="txtName" ControlToCompare="123"   ErrorMessage=" The user is already registered "  Operator="NotEqual"   type="String"  ForeColor="red"></ASP:CompareValidator>

3. RangeValidator: Scope validation.Verify that a field is in a range.

In the following example, the text box is entered between the minimum and maximum values, and an error occurs if the maximum or minimum values are exceeded.The code is as follows:


<asp:TextBox ID="num_id" runat="server" BackColor="White"></asp:TextBox>
<asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage=" Numbered as 1~1000 Between " ControlToValidate="num_id" MaximumValue="1000" MinimumValue="1" Type="Integer"></asp:RangeValidator>

4. RegularExpressionValidator: Regular expression validation.It validates the validity of the format of user input fields based on regular expressions, such as e-mail, identity cards, phone numbers, and so on.

The following example is a text box input that meets the requirements of a regular expression in ValidationExpression and fails if it does not.The code is as follows:


<asp:TextBox ID="txtMail" runat="server" BackColor="White"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ForeColor="Red" runat="server" ErrorMessage=" Please enter the correct mailbox " ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtMail"></asp:RegularExpressionValidator>

The above is the whole content of this article, I hope you like it.


Related articles: