Asp.netweb Server validation controls how to use _ Practical tips

Source: Internet
Author: User

What is a validation control?

You want the user to enter the correct type of data, and to verify that user input satisfies the requirements, you must check the value, range, or format entered.
. Where is the validation control in net?

In the validation group for the Toolbox.
How many asp.net controls are commonly used? What's the difference?

1) CompareValidator control: Compared to the given value.
2) CustomValidator control: User Custom validation logic
3) RangeValidator Control: Checks whether the value of the control is within a given valid range.
4 Regularexpressvalidator Control: Use regular expressions to verify that the data entered by the user conforms to the predefined format.
5 RequiredFieldValidator Control: Prevents users from entering null values.
6) ValidationSummary Control: summarizes error messages for all validation controls on the Web page.
The following:
All validation controls are inherited from the base class BaseValidator and provide some public properties for the validation control:

How do I use asp.net validation controls?

As with adding other server controls, you can enable validation of user input.
In the Controltovalidator property of the control, give the ID number of the validation control to associate.
client authentication? server Authentication?

Client-side validation
For the user experience, to prevent people from submitting many times to fill in the wrong such as some legitimate checks, such as string length, illegal characters, regular expressions, non-empty and so on. Most are friendly hints (inherently unreliable, but responsive.) )
Server-side validation
Business logic, security needs of real validation, such as the password is correct, need to write code in the background to have permissions and so on. Where the data needs to be submitted and saved, put it on the server for verification. (For security, client-side validation is also verified by the server end.) )
Mixed authentication:
Both the client and the server need authentication.
DEMO:
This basically said that the client authentication and the server authentication two kinds of ways, take the CustomValidator control as an example:
To verify that the user entered an even number
Client Authentication:
The Javascrip code is used here:

<script language= "javascript" type= "Text/javascript" > 
          /* Verify that it is an even function * 
    /function clientvalidate (source, args)  
    { 
 
      if (args.value% 2) = = 0) 
          args. IsValid = true; 
      else 
          args. IsValid = false; 
    } 
         
  </script> 

Client-side validation control code:

<asp:customvalidator id= "CustomValidator1" runat= "controltovalidate=" Txtos "errormessage="  
 
    is not an even number! "Clientvalidationfunction=" Clientvalidate "></asp:CustomValidator> 

Server Authentication, C # code:

public partial class _default:system.web.ui.page 
{ 
 
  //server-side validation input is an even number. 
  protected void Cvinput_servervalidate (object source, ServerValidateEventArgs args) 
  { 
    //Get validation control input value. 
    int value = Int. Parse (args. Value); 
    Determine if it is an even number. 
    if ((value% 2) = = 0) 
    { 
      args. IsValid = true; 
    } 
    else 
    { 
      args. IsValid = false;}}} 
 

Server Authentication, control code:

<asp:customvalidator id= "Cvinput" runat= "Server"  
      

The resulting results are shown below:


Compared with the CS control, there is no validation control in the CS control Oh.

Learning the use of the master, user control, and today learned the Asp.netweb server validation control method of use, I hope that each article has been harvested.

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.