Advertisement
Guest User

Custom Date Validator

a guest
Dec 21st, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
  2.     public sealed class ValidateDODDOB : ValidationAttribute
  3.     {
  4.         // Error Message
  5.         private const string DefaultErrorMessage = "Please type the date in the format specified.";
  6.  
  7.         // Gets or sets the Regular expression.
  8.         private Regex Regex { get; set; }        
  9.  
  10.         // The pattern used for Date of Birth and Date of Death validation.
  11.         public string Pattern { get { return @"(\d+\s?)?(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)?\s(\d+\s)?(BCE)?"; } }
  12.  
  13.         // Initializes a new instance of the VerifyDODDOB class.
  14.         public ValidateDODDOB() : base(DefaultErrorMessage)
  15.         {
  16.             this.Regex = new Regex(this.Pattern);
  17.         }
  18.  
  19.         // Determines whether the specified value of the object is valid.
  20.         // true if the specified value is valid; otherwise, false.
  21.         public override bool IsValid(object value)
  22.         {
  23.             // convert the value to a string
  24.             var stringValue = Convert.ToString(value);
  25.  
  26.             var m = Regex.Match(stringValue);
  27.  
  28.             return m.Success;
  29.         }
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement