zoukankan      html  css  js  c++  java
  • 使用微软企业库数据验证

    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("数据验证失败");
            }
        }
    }

  • 相关阅读:
    Linux and the Unix Philosophy(6)
    在docker环境下部署kafka集群
    Docker学习笔记1-docker介绍及安装
    nginx安装
    centos7下NFS配置
    搭建kafka集群
    nexus开机启动
    java跨库事务Atomikos
    mongodb数据库还原
    Linux下运行jar
  • 原文地址:https://www.cnblogs.com/sobaby/p/1269761.html
Copyright © 2011-2022 走看看