I believe that people with experience in ASP. NET programming will not be unfamiliar with RequiredFieldValidator. This control is used to prohibit the input of specified content (that is, the value of the InitialValue attribute. The default value of this attribute is a null string, which is often used to verify whether the content entered by users is null .). In addition, spaces at both ends of the input content are automatically removed before verification.
Let's take a look at the common attributes of RequiredFieldValidator:
Attribute
Description
BackColor
Background Color
ControlToValidate
Id of the verified Control
Display
Display behavior of error messages in the control.
· None-verification messages are never displayed inline.
· Static-allocate space in the page layout for displaying verification messages.
· Dynamic-If verification fails, the space used to display verification messages is dynamically added to the page.
EnableClientScript
Boolean value that specifies whether to enable client verification. True indicates enabled, and false indicates disabled.
Enabled
Boolean value that specifies whether to enable the verification control. True indicates enabled, and false indicates disabled.
ErrorMessage
Text displayed in the ValidationSummary control when verification fails.
Note: If the Text property is not set, the Text is displayed in the verification control.
ForeColor
The foreground color of the widget. That is, the font color of the error message.
Id
The unique id of the control.
InitialValue
Specifies the initial value (start value) of the input control ). The default value is a null string. It indicates the value that you do not want the user to enter in the input control.
IsValid
Boolean value indicating whether the associated input control has passed verification. True indicates pass, and false indicates fail.
Runat
Specifies that the control is a server control. Must be set to "server ".
Text
Message displayed when verification fails.
After reading the common attributes of RequiredFieldValidator, let's make a small instance to practice it.
[Html]
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "the user name cannot be blank. aspx. cs" Inherits = "WebApplication1. the user name cannot be blank 1" %>
<! DOCTYPE html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> User name cannot be blank </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Asp: Label ID = "lbUserName" runat = "server" Text = "username:"> </asp: Label>
<% -- Receive user name -- %>
<Asp: TextBox ID = "txtUserName" runat = "server"> </asp: TextBox>
<% -- Verify whether the user name is null -- %>
<Asp: RequiredFieldValidator ID = "RequiredFieldValidator1" runat = "server" ForeColor = "Red" text = "username cannot be blank" ControlToValidate = "txtUserName"> </asp: RequiredFieldValidator
<Br/>
<Br/>
<% -- Submit information for the server to verify whether the input meets the requirements -- %>
<Asp: Button ID = "btnSubmit" runat = "server" Text = "Submit"/>
</Form>
</Body>
</Html>
If you do not enter any content or enter only spaces, click the right side of the submit text box to display several very eye-catching Red prompts, "user name cannot be blank.
Here we use the default verification value of the control, that is, it cannot be blank. Of course, you can also set other content that is not allowed to be input as needed, just make the attribute InitialValue = "value not allowed to be input.
RequiredFieldValidator is easy to use. You only need to know its common attributes to meet our basic needs. Although it is simple, it is often used. So let's sort it out.