使用微软企业库-数据验证
using System;
using System.Text.RegularExpressions;
//
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;
/// <summary>
///使用微软企业库-数据验证
/// </summary>
public class EnterpriseValidation
{
[StringLengthValidator(1, 10)]
string deptName;
public string DeptName
{
set { deptName = value; }
get { return "单位名称是:" + deptName; }
}
[StringLengthValidator(1, 50, Ruleset = "RuleSetA", MessageTemplate = "First Name must be between 1 and 50 characters long")]
public string FirstName
{
get;
set;
}
[StringLengthValidator(1, 50, Ruleset = "RuleSetA", MessageTemplate = "Last Name must be between 1 and 50 characters long")]
public string LastName
{
get;
set;
}
[RelativeDateTimeValidator(-120, DateTimeUnit.Year, -18, DateTimeUnit.Year, Ruleset = "RuleSetA", MessageTemplate = "Must be 18 years old")]
public DateTime DateOfBirth
{
get;
set;
}
[RegexValidator(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", Ruleset = "RuleSetA")]
public string Email
{
get;
set;
}
}
public class CallValidation
{
public void Add()
{
EnterpriseValidation dept = new EnterpriseValidation();
dept.DeptName = "aaaaaaaaaab";
ValidationResults result = Validation.Validate<EnterpriseValidation>(dept);
if (result.IsValid)
{
throw new InvalidOperationException("数据验证失败");
}
}
}