Encapsulation and application of data verification in asp.net

Source: Internet
Author: User
Our business system involves a lot of form editing and verification. The simplest way is to use the data verification control provided by asp.net, but there are three problems as follows:

1. The verification control should be added to the form during design and manually set attributes such as data type and error information, which is cumbersome.
2 for an input box, we may want to check multiple items, such as: required, must be in the date format, and must be greater than 2007-1-1. In this way, you need to add multiple validation controls.
3. Binding business rules and forms is not conducive to maintenance or reuse. The expected verification function is: 1. The verification rule is separated from the form, and the input box is displayed on the form. The verification control is dynamically created according to the configuration file during running.
2. Generate the default value based on the verification rules: it cannot be blank. The error number type must be in the range of 1 to 100.
3 flexible validation rule expressions: for example, range (1,200),> = 0.5, Mail (), Mobile (). Not Supported currently
The conditions such as and or are easy to expand. To this end, we encapsulate this part of function. When using this function, you only need to set the control name, data type, and whether it is required in the xml file to verify the expression information.
<ValidateInfo>
<ControlName> Number of ports </ControlName>
<DataType> Integer </DataType>
<Require> true </Require>
<Expression> range (1,100) </Expression>/Files/BoKeRen/validatetest.rar
</ValidateInfo>

In the Page_Init event of page, call the class method:
ValidateHelper. LoadFromFile ("data validation _ configuration. xml.

Class Design:
// Configure the Information Class
Class ValidateInfo
{
Public string ControlName;
Public ValidationDataType DataType;
Public string ErrorMessage;
Public bool Require;
Public string Expression;
}
 
// Check the control to create a factory
Public class ValidatorFactory
{
CreateRequiredFieldValidator ();
CreateDateTypeCheckValidator ();
CreateRangeValidator (string min, string max );
CreateCompareValidator (ValidationCompareOperator success, string valueToCompare );
...
}

// Check the rule parser and create a check control based on the configuration information
Public class ValidatorParser
{
Parse (ValidateInfo info, ValidatorFactory factory );
...
}

// Read the configuration information and bind the validation control to the form
Public class ValidateHelper
{
LoadFromFile (Page page, string fileName)
}

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.