A
Asp. NET powerful embodiment is the rich Web controls, here we talk about one of the-validation controls
。 It summarizes a variety of web data validation methods, designed to be the common form of asp.net control today.
Asp. NET public six kinds of validation controls, respectively, are as follows:
Control Name features describe
RequiredFieldValidator (mandatory field validation) is used to check for input values
CompareValidator (comparison validation) compares two inputs by setting
RangeValidator (range validation) Enter whether the specified range
RegularExpressionValidator (regular expression validation) regular expression validation control
CustomValidator (custom validation) custom validation controls
ValidationSummary (validation Summary) Summary validation Results
--------------------------------------------------------------------------------
Directory
1. Introduction to validation controls
2. Verifying how controls are used
3. Summary
--------------------------------------------------------------------------------
1. Introduction to validation controls
As the name suggests, validation controls are controls that validate the correctness of user input data, such as when users enter data in a text box
It displays a message indicating that the validated data is illegal. The validation process can be performed both on the server and
Can be executed on the client computer, and the validation code running on the client is executed before the data is submitted, so you can increase the process
The performance of the order. Asp. NET validation controls can perform multiple checksums, such as data range checking, comparison between data, and customization
checksums, and so on, the following sections will describe each of these validation controls.
--------------------------------------------------------------------------------
2. Verifying how controls are used
①requiredfieldvalidator Control
RequiredFieldValidator works when the controls on the page require that data be entered.
The ControlToValidate property selects the control that needs to be validated, and the ErrorMessage property is displayed after the checksum is not valid
Error message.
<HTML>
<HEAD>
<title>requiredfieldvalidator example</title>
</HEAD>
<BODY>
<form id= "Form1" runat= "Server" >
Name:
<asp Tutorial: TextBox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:requiredfieldvalidator id= "RequiredFieldValidator1" runat= "Server"
Errormessage= "Please enter your name"
Controltovalidate= "TextBox1" ></asp:RequiredFieldValidator>
<asp:button id= "Button1" runat= "Server" text= "button" ></asp:Button>
</form>
</BODY>
</HTML>
In the above standard code:
ControlToValidate: Indicates that the control ID is to be checked;
ErrorMessage: Indicates an error message that occurs when a check is not valid;
Display: The way the error message is displayed; static indicates that the error message of the control occupies a positive position in the page;
Dymatic indicates that the control error message takes up the page control when it appears, and none indicates that the error occurred, but can be found in the
Validatorsummary in the display;
PLACEHOLDER: When display is static, the error message occupies a page space as large as "placeholder"
②comparevalidator Control
The CompareValidator control is used to compare data consistency between two input controls and can also be used to validate
The data type of the content in the control: such as shaping, string, and so on. ControlToCompare and ControlToValidate Properties
The two controls that are used to set the comparison.
<HTML>
<HEAD>
<title>comparevalidator example</title>
</HEAD>
<BODY>
<form id= "Form1" runat= "Server" >
<p>password:
<asp:textbox id= "txtpwd" runat= "Server"
Textmode= "Password" ></asp:TextBox><BR>
Confirm:
<asp:textbox id= "txtcfm" runat= "Server"
Textmode= "Password" ></asp:TextBox></P>
<P>
<asp:button id= "Button2" runat= "Server" text= "button" ></asp:Button>
<asp:comparevalidator id= "CompareValidator1" runat= "Server"
Errormessage= "Password error! "
Controltovalidate= "Txtcfm"
Controltocompare= "Txtpwd" ></asp:CompareValidator></P>
</form>
</BODY>
</HTML>
Compare controls to compare the input of two controls with the program settings, we do not just understand the comparison as "equal", although
Equality is the most used, in fact, the comparison here includes a wide range, you can see the standard code will understand.
The standard code for comparing controls is as follows:
In the above standard code:
Type represents the data type of the control to be compared;
Operator represents comparison operations (that is, why the comparison is more than just "equal"), here,
There are 7 ways of comparison;
Other attributes are the same as RequiredFieldValidator;
Here, pay attention to the difference between ControlToValidate and ControlToCompare, if operate is
Greatethan, then, must be controltocompare greater than controltovalidate is lawful, and this, should
Do you understand the meaning of both of them? Example program please refer to the RequiredFieldValidator control, control the standard code from
Designed.
③rangevalidator Control
The RangeValidator control can be used to determine whether the value entered by the user is within a specific range, and the property MaximumValue
And MinimumValue are used to set the maximum and minimum values of the range.
<HTML>
<HEAD>
<title>rangevalidator example</title>
</HEAD>
<BODY>
<form id= "Form1" runat= "Server" >
<p>age:
<asp:textbox id= "txtage" runat= "Server" ></asp:TextBox>
<asp:rangevalidator id= "RangeValidator1" runat= "Server"
Controltovalidate= "Txtage"
Errormessage= "Age error! "Maximumvalue=" 99 "
Minimumvalue= "1" ></asp:RangeValidator></P>
</form>
</BODY>
</HTML>
Use MinimumValue and MaximumValue to define the range of control input, and type to define the types of control input values
。
④regularexpressionvalidator Control
The RegularExpressionValidator control can determine whether the user has entered the correct expression, such as the phone number, postal code
, URLs, and so on, the ControlToValidate property selects the control that needs to be validated, and the ValidationExpression property writes
The style of the expression that needs to be validated, the following example is a code that verifies the ZIP code.
<HTML>
<HEAD>
<title>regularexpressionvalidator Exa mple</title>
</HEAD>
<BODY>
<form id= "Form1" runat= "Server" >
<p>postal Code:
<asp:textbox id= "Txtpostalcode" runat= "Server" ></asp:TextBox>
<asp:regularexpressionvalidator id= "RegularExpressionValidator1" runat= "Server"
& nbsp; controltovalidate= "Txtpostalcode" errormessage= "Postal Code error! "
validationexpression=" d6} "></asp: regularexpressionvalidator></p>
</form>
</BODY>
</HTML>
In the above standard code, ValidationExpression is the focus, now look at its construction:
In ValidationExpression, different characters represent different meanings:
"." Represents any character;
The expression "*", together with other expressions, represents an easy combination;
"[A-z]" denotes any uppercase letter;
"D" means an easy number;
Note that in the above expression, the quotation marks are not included;
Example:
Regular expression: ". *[a-z]" denotes any combination of characters at the beginning of a number followed by an uppercase letter
⑤customvalidator Control
The CustomValidator control is used to perform user-defined validation, which can be either a server-side or a guest
User-side, the following code is an example of using client-side validation of ZIP codes.
<HTML>
<HEAD>
<title>customvalidator example</title>
</HEAD>
<BODY>
<form id= "Form1" runat= "Server" >
<p>postal Code:
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:customvalidator id= "CustomValidator1" runat= "Server"
Controltovalidate= "TextBox1"
Errormessage= "CustomValidator"
clientvalidationfunction= "Clientvalidate" >
</asp:CustomValidator></P>
</form>
</BODY>
</HTML>
<script language= "VBScript" >
Sub clientvalidate (source, arguments)
If isnumeric (arguments. Value) and Len (arguments. Value) =6 Then
Arguments. Isvalid=true
Else
Arguments. Isvalid=false
End If
End Sub
</script>
⑥validationsummary Control
This control prints all the checksum errors in the page as a list, and the list is displayed by the DisplayMode property
Reset
<title>validationsummary example</title>
</HEAD>
<body>
<form id= "Form1" runat= "server"
<p>age:
<asp:textbox id= "txtage" runat= "Server" ></ASP:TEXTBOX>
<asp: RequiredFieldValidator id= "RequiredFieldValidator1" runat= "Server"
controltovalidate= "Txtage" Errormessage= "Age error! "
Display= "None" ></asp:RequiredFieldValidator><BR>
Postal Code:
<asp:textbox id= "Txtpostalcode" runat= "Server" ></asp:TextBox>
<asp:requiredfieldvalidator id= "RequiredFieldValidator2" runat= "Server"
Controltovalidate= "Txtpostalcode" errormessage= "Postal Code error! "
Display= "None" ></asp:RequiredFieldValidator></P>
<P>
<asp:button id= "Button1" runat= "Server" text= "button" ></asp:Button></P>
<P>
<asp:validationsummary id= "ValidationSummary1" runat= "Server"
headertext= "You must enter a value in the following
Fields: "></asp:ValidationSummary></P>
</form>
</body>
</HTML>
To take a look at a control instance
using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Drawing;
6 using System.Drawing.Imaging;
7 using System.Web.SessionState;
8 Namespace WebApp
9 {
Ten///<summary>
11 Summary description of///Verification code instance
///</summary>
Public class authentication Code instance: IHttpHandler, IRequiresSessionState//in general handler
Use session to implement the interface in System.Web.SessionState;
14 {
public void ProcessRequest (HttpContext context)
16 {
Context.response.contenttype = "Image/jpeg"; Returns the type of JPG;
The using (bitmap bitmap = new Bitmap (140, 80))//pixel size;
19 {
using (Graphics g = graphics.fromimage (bitmap))//Generate an artboard
21 {
Random rand = new Random ();
int code = RAND.NEXT (1000, 999999); A random function is developed to qualify the character random string size;
String strcode = Code.tostring ();
httpcontext.current.session["code"] = Strcode; Using the session connection in a generic handler
Mouth
G.clear (Color.yellowgreen); Specifies the background color;
g.DrawString (Strcode, New Font ("Blackish"), Brushes.white, New pointf (15,
25)); Picture related parameters, (font, size), color, position;
Bitmap.save (Context.response.outputstream, imageformat.jpeg); Output to the stream and protect
Save as JPG format;
29}
30}
31}