Asp. NET validation control (turn)

Source: Internet
Author: User
Tags define commit comparison expression header integer uppercase letter valid
Asp.net| control ASP. NET validation Control Hsiang solution

Asp. NET is Microsoft's next generation of web development tools, its powerful features immediately attracted a large number of Web developers to invest in it. Now, let's take a look at the ASP.net validation controls, and feel the power of the asp.net, while facilitating our current web development.

Web developers, especially ASP developers, have been annoyed with data validation, and when you finally write the main body of the data submission program, you have to spend a lot of time validating the legality of each user input. If developers are familiar with JavaScript or VBScript, you can use these scripting languages to easily validate, but also consider whether the user's browser supports these scripting languages, and if you are not familiar with these or want to support all user browsers, you must verify in the ASP program, But this validation increases the burden on the server. Now, with ASP.net, you can not only easily validate user input, but also choose to verify on the server side or on the client, no longer need to consider so much, programmers can focus on the design of the main program.

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


Now let's look at the use of these controls

One: The use of RequiredFieldValidator (must field validation)

The standard code used by the RequiredFieldValidator control is as follows:

<asp:requiredfieldvalidator id= "Validator_name" runat= "Server"
Controltovalidate= "Name of control to check"
Errormessage= "error Message"
Display= "static| dymatic| None "
>
Placeholder
</asp:requiredfieldvalidator >

 


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 on the page; dymatic indicates that the control error message takes up the page control when it appears; none is displayed when the error occurs, but it can be displayed in the validatorsummary;

PLACEHOLDER: When display is static, the error message occupies a page space of "placeholder" so large;

Now, let's look at an example:
 

<asp:textbox id= "txtname" runat= "Server"/>
<asp:requiredfieldvalidator id= "Validator1" runat= "Server"
Controltovalidate= "Txtname"
Errormessage= "Name must be entered"
Display= "Static" >
* Name must be entered
</ASP:RequiredFieldValidator>
 

In the above example, check that the Txtname control has input, and if not, display the error message "name must be entered." Is it simple?
Note: The code for the above code and the other controls below is best placed in the form, and unlike the ASP, the form is best written like this:

 

<form runat= "Server" >
Other code
</Form>
 


 

In this way, the form is executed on the server side, and the commit is valid;

Two: CompareValidator (comparison validation) control

Compare controls to compare the input of two controls to match the program settings, we do not just understand the comparison is "equal", although the same is the most used, in fact, the comparison here covers a wide range, we see the standard code will understand.

The standard code for comparing controls is as follows:
 

<asp:comparevalidator id= "validator_id" runat= "Server"
Controltovalidate= "The control ID to validate"
Errormessage= "error Message"
Controltocompare= "The control ID to compare"
Type= "String| integer| double| datetime| Currency "
Operator= "equal| notequal| greaterthan| greatertanequal| lessthan| lessthanequal| DataTypeCheck "
Display= "static| dymatic| None "
>
Placeholder
</ASP:CompareValidator>
 


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 not just "equal" reason), here, compared to 7 ways;

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 legitimate, this, should understand the meaning of both of them? Example program please refer to the RequiredFieldValidator control and design it against the standard code.

Three: RangeValidator (scope validation) control

Verify that the input is in a certain range, the range is determined with MaximumValue (max) and Minimunvlaue, and the standard code is as follows:

<asp:rangevalidator id= "vaidator_id" runat= "Server"
Controltovalidate= "The control ID to validate"
Type= "Integer"
Minimumvalue= "Minimum Value"
Maximumvalue= "Max"
Errormessage= "error Message"
Display= "static| dymatic| None "
>
Placeholder
</ASP:RangeValidator>
 


In the above code:

Use MinimumValue and MaximumValue to define the range of control input, and type to define the types of control input values.

Four: Regularexpresionvalidator (regular expression) control

The regular expression validation control is very powerful, you can easily construct the verification method, we first look at the standard code:

<asp:regularexpressionvalidator id= "validator_id" runat= "Server"
Controltovalidate= "To verify control name"
Validationexpression= "Regular expression"
Errormessage= "error Message"
Display= "Static"
>
Placeholder
</ASP:RegularExpressionValidator>
 


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 a number easily;

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 the number followed by an uppercase letter.

Five: ValidationSummary (validation summary) control

The control collects all validation error messages on this page and can organize them to be displayed later. The standard code is as follows:

<asp:validationsummary id= "validator_id" runat= "Server"
headertext= "header Information"
Showsummary= "true| False "
Diaplaymode= "list| Bulletlist| Singleparagraph "
>
</asp:validationsummary >
 

In the above standard code, Headtext corresponds to the table's Headtext,displaymode indicating how the error message is displayed: The list corresponds to <BR> in HTML; bulletlist equivalent to HTML <LI> Singleparegraph indicates that there is no segmentation between error messages;

VI: CustomValidator (custom validation) control

The control uses a custom function to define the authentication method, and its standard code is as follows:
 

<asp:customvalidator id= "validator_id" runat= "Server"
Controltovalidate= "Controls to validate"
onservervalidatefunction= "Validation Function"
Errormessage= "error Message"
Display= "static| dymatic| None "
>
Placeholder
</asp:customvalidator >
 


In the above code, the user must define a function to validate the input.

VII: summary

Asp. NET validation controls have been introduced, you can find that they are very powerful, and no longer worry about verification. Of course, the above introduction is not particularly detailed, subtle places, but also invite you to read the Microsoft SDK.
Asp. NET is Microsoft's next generation of web development tools, its powerful features immediately attracted a large number of Web developers to invest in it. Now, let's take a look at the ASP.net validation controls, and feel the power of the asp.net, while facilitating our current web development.

Web developers, especially ASP developers, have been annoyed with data validation, and when you finally write the main body of the data submission program, you have to spend a lot of time validating the legality of each user input. If developers are familiar with JavaScript or VBScript, you can use these scripting languages to easily validate, but also consider whether the user's browser supports these scripting languages, and if you are not familiar with these or want to support all user browsers, you must verify in the ASP program, But this validation increases the burden on the server. Now, with ASP.net, you can not only easily validate user input, but also choose to verify on the server side or on the client, no longer need to consider so much, programmers can focus on the design of the main program.

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


Now let's look at the use of these controls

One: The use of RequiredFieldValidator (must field validation)

The standard code used by the RequiredFieldValidator control is as follows:

<asp:requiredfieldvalidator id= "Validator_name" runat= "Server"
Controltovalidate= "Name of control to check"
Errormessage= "error Message"
Display= "static| dymatic| None "
>
Placeholder
</asp:requiredfieldvalidator >

 


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 on the page; dymatic indicates that the control error message takes up the page control when it appears; none is displayed when the error occurs, but it can be displayed in the validatorsummary;

PLACEHOLDER: When display is static, the error message occupies a page space of "placeholder" so large;

Now, let's look at an example:
 

<asp:textbox id= "txtname" runat= "Server"/>
<asp:requiredfieldvalidator id= "Validator1" runat= "Server"
Controltovalidate= "Txtname"
Errormessage= "Name must be entered"
Display= "Static" >
* Name must be entered
</ASP:RequiredFieldValidator>
 

In the above example, check that the Txtname control has input, and if not, display the error message "name must be entered." Is it simple?
Note: The code for the above code and the other controls below is best placed in the form, and unlike the ASP, the form is best written like this:

 

<form runat= "Server" >
Other code
</Form>
 


 

In this way, the form is executed on the server side, and the commit is valid;

Two: CompareValidator (comparison validation) control

Compare controls to compare the input of two controls to match the program settings, we do not just understand the comparison is "equal", although the same is the most used, in fact, the comparison here covers a wide range, we see the standard code will understand.

The standard code for comparing controls is as follows:
 

<asp:comparevalidator id= "validator_id" runat= "Server"
Controltovalidate= "The control ID to validate"
Errormessage= "error Message"
Controltocompare= "The control ID to compare"
Type= "String| integer| double| datetime| Currency "
Operator= "equal| notequal| greaterthan| greatertanequal| lessthan| lessthanequal| DataTypeCheck "
Display= "static| dymatic| None "
>
Placeholder
</ASP:CompareValidator>
 


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 not just "equal" reason), here, compared to 7 ways;

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 legitimate, this, should understand the meaning of both of them? Example program please refer to the RequiredFieldValidator control and design it against the standard code.

Three: RangeValidator (scope validation) control

Verify that the input is in a certain range, the range is determined with MaximumValue (max) and Minimunvlaue, and the standard code is as follows:

<asp:rangevalidator id= "vaidator_id" runat= "Server"
Controltovalidate= "The control ID to validate"
Type= "Integer"
Minimumvalue= "Minimum Value"
Maximumvalue= "Max"
Errormessage= "error Message"
Display= "static| dymatic| None "
>
Placeholder
</ASP:RangeValidator>
 


In the above code:

Use MinimumValue and MaximumValue to define the range of control input, and type to define the types of control input values.

Four: Regularexpresionvalidator (regular expression) control

The regular expression validation control is very powerful, you can easily construct the verification method, we first look at the standard code:

<asp:regularexpressionvalidator id= "validator_id" runat= "Server"
Controltovalidate= "To verify control name"
Validationexpression= "Regular expression"
Errormessage= "error Message"
Display= "Static"
>
Placeholder
</ASP:RegularExpressionValidator>
 


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 a number easily;

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 the number followed by an uppercase letter.

Five: ValidationSummary (validation summary) control

The control collects all validation error messages on this page and can organize them to be displayed later. The standard code is as follows:

<asp:validationsummary id= "validator_id" runat= "Server"
headertext= "header Information"
Showsummary= "true| False "
Diaplaymode= "list| Bulletlist| Singleparagraph "
>
</asp:validationsummary >
 

In the above standard code, Headtext corresponds to the table's Headtext,displaymode indicating how the error message is displayed: The list corresponds to <BR> in HTML; bulletlist equivalent to HTML <LI> Singleparegraph indicates that there is no segmentation between error messages;

VI: CustomValidator (custom validation) control

The control uses a custom function to define the authentication method, and its standard code is as follows:
 

<asp:customvalidator id= "validator_id" runat= "Server"
Controltovalidate= "Controls to validate"
onservervalidatefunction= "Validation Function"
Errormessage= "error Message"
Display= "static| dymatic| None "
>
Placeholder
</asp:customvalidator >
 


In the above code, the user must define a function to validate the input.

VII: summary

Asp. NET validation controls have been introduced, you can find that they are very powerful, and no longer worry about verification. Of course, the above introduction is not particularly detailed, subtle places, but also invite you to read the Microsoft SDK.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.