SoFunction
Updated on 2025-03-07

Simple use of C# validator

Provide developers with a simple and practical set of server controls to verify whether the information entered by the user is valid. The main properties of these controls are id (the unique id of the control), ControlToValidate (the id of the control being verified), ErrorMessage (the text displayed in the control when verification fails), and runat (which stipulates that the control is a server control. It must be set to "server").

1. RequiredFieldValidator: Verify a required field. If this field is not filled, information will not be submitted.

The following example shows the verification of whether the input is empty in the text box. If the input content is empty, an error will be reported. The code is as follows:

Copy the codeThe code is as follows:

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

2. CompareValidator: Comparative verification. Compare whether the values ​​of the two fields are equal, such as whether the two fields of password and confirm password are equal; compare one field with a specific value.

The following example shows the password verification of the input of two text boxes. If the input content of the two text boxes is inconsistent, the error will be reported. The code is as follows:

Copy the codeThe 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="two password inputs are inconsistent" ControlToValidate="txtPWD1" ControlToCompare="txtPWD2"   type="String"></asp:CompareValidator>

The following example is to verify the input content value in the text box. If the input content is equal to a certain value, an error will be reported. The code is as follows:

Copy the codeThe code is as follows:

<ASP:TextBox RunAt="Server"/>
<ASP:CompareValidator Runat="Server"  ControlToValidate="txtName" ControlToCompare="123"  ErrorMessage="This user has been registered" Operator="NotEqual" type="String"  ForeColor="red"></ASP:CompareValidator>

3. RangeValidator: Range Verification. Verify that a field is in a range.

The following example shows that the content entered in the text box is between the maximum and minimum values. If the maximum or minimum values ​​are exceeded, an error will be reported. The code is as follows:

Copy the codeThe code is as follows:

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

4. RegularExpressionValidator: Regular Expression Verification. It verifies whether the format of the user input field is legal, such as email, ID card, phone number, etc.

The following example shows that the input content in the text box meets the requirements of the regular expression in ValidationExpression. If the requirements do not meet the requirements, the error will be reported. The code is as follows:

Copy the codeThe 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 email" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtMail"></asp:RegularExpressionValidator>

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