Asp. Select validation of form input rows in net

Source: Internet
Author: User

In ASP.net, there is a series of validation controls that make it easy to verify that the data entered by the user is legitimate. Although these validation controls are powerful and easy to use, they have one drawback: generally, when using them, the user input to the entire page is validated when the page is submitted. In a validation control, there is no direct way to validate only the input of parts of a page. This article describes how to use the ASP.net validation controls and JavaScript features to complete the ability to validate only the input of parts of a page.

Why do you have to validate the input of some parts of a page? For example, in some applications, sometimes for the convenience of user input, it is possible that on the same page, there are multiple forms that require users to input data and submit data (this is common in some information system background management systems). And the user does one operation at a time, for example, the user input each purchase quantity and submit, then only need to validate the submitted data, and other forms of the page, such as the number of each shipment, because the user did not fill in the data, you do not need to check it, this also improves the efficiency of the application.

Let's take a look at how to selectively validate form input, beginning with a comprehensive understanding of some of the properties and usages of the input validation controls in the next asp.net.

The first introduction is the CausesValidation property. If we do not want to validate the events submitted by a button, simply set the CausesValidation property to False, for example, with a Cancel button, you can set this:

<asp:button id="cmdCancel" runat="server" Text="Cancel" CausesValidation="False"></asp:button>

Another way to do this is to:

cmdcancel.causesvalidation=false;

This way, once the CausesValidation property is set to False, the validation of either the client or the server will not work.

If you want to selectively validate portions of a page, you will need to implement some of the methods and javascrpt of the validation control, and the following list lists some of the methods and properties in the validation control:

Name Describe
Page_isvalid Verify that all input in the page is valid and return a Boolean value
Page_Validators An array of all validation controls in the current page
Page_validationactive is a Boolean value that indicates whether validation is performed, and setting to false turns off the validation feature.
Isvalid This property verifies that the client's input is legitimate
Validatorenable (Val, enable) This method takes a validation control as a pass-through parameter, starts or prevents the control from using the validation feature, and works on the client

This is a special introduction to the Validatorenable method, which prevents a control from using validation features, such as:

<script language="javascript">
 ValidatorEnable(验证控件名称, false)
</script>

If you want to prevent all validation controls in the page from using the validation feature, you can match the page_validators array (the elements in the array are all validation controls on the page), using the following code

<script language="javascript">
for(i=0;i< Page_Validators.length;i++)
{
 ValidatorEnable(Page_Validators[i], false)
}
</script>

In the actual use, the above code together, you can implement a selective form input validation, we first look at a specific example, the following figure:

In the figure above, what we're going to do is that when the user simply enters the value of region name, the server side only verifies the input of region name after clicking on the "Get Reports" button, and the user does not use the start date and end date entered, and no checksum is performed. The error message is also not displayed, whereas when the user enters the start and end dates and commits, the system does not validate the input for region name.

The code is as follows.

<script language="javascript">
function enableRegionValidators()
{
 for(i=0;i< Page_Validators.length;i++)
 {
  ValidatorEnable(Page_Validators[i], false)
 }
 ValidatorEnable(rvRegion, true)
}
</script>

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.