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)
}