#if FRAMEWORK20
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Text.RegularExpressions;
namespace DevComponents.DotNetBar.Validator
{
    /// 
    /// Represents the regular expression validator used with SuperValidator control.
    /// 
    public class RegularExpressionValidator : ValidatorBase
    {
        #region Implementation
        /// 
        /// Initializes a new instance of the RegularExpressionValidator class.
        /// 
        public RegularExpressionValidator()
        {
        }
        /// 
        /// Initializes a new instance of the RegularExpressionValidator class.
        /// 
        /// 
        public RegularExpressionValidator(string errorMessage, string validationExpression)
        {
            _ValidationExpression = validationExpression;
            this.ErrorMessage = errorMessage;
        }
        /// 
        /// Initializes a new instance of the RegularExpressionValidator class.
        /// 
        /// 
        public RegularExpressionValidator(string errorMessage, string validationExpression, bool emptyValueIsValid)
        {
            _ValidationExpression = validationExpression;
            this.ErrorMessage = errorMessage;
            this.EmptyValueIsValid = emptyValueIsValid;
        }
         /// 
        /// Initializes a new instance of the RegularExpressionValidator class.
        /// 
        /// 
        public RegularExpressionValidator(string errorMessage, string validationExpression, string optionalValidationGroup, bool emptyValueIsValid)
        {
            _ValidationExpression = validationExpression;
            this.ErrorMessage = errorMessage;
            this.OptionalValidationGroup = optionalValidationGroup;
        }
        private string _ValidationExpression = "";
        /// 
        /// Gets or sets regular expression used to validate controls value.
        /// 
        [DefaultValue(""), Category("Behavior"), Description("Indicates regular expression used to validate controls value."), Editor("DevComponents.DotNetBar.Design.ValidationExpressionEditor, DevComponents.DotNetBar.Design, Version=14.1.0.37, Culture=neutral,  PublicKeyToken=90f470f34c89ccaf", typeof(System.Drawing.Design.UITypeEditor))]
        public string ValidationExpression
        {
            get { return _ValidationExpression; }
            set
            {
                _ValidationExpression = value;
            }
        }
        private bool _EmptyValueIsValid = false;
        /// 
        /// Gets or sets whether empty value is considered valid value by the validator. Default value is false.
        /// 
        [DefaultValue(false), Category("Behavior"), Description("Indicates whether empty value is considered valid value by the validator")]
        public bool EmptyValueIsValid
        {
            get { return _EmptyValueIsValid; }
            set
            {
                _EmptyValueIsValid = value;
            }
        }
        
        public override bool Validate(System.Windows.Forms.Control input)
        {
            if (string.IsNullOrEmpty(_ValidationExpression))
            {
                this.LastValidationResult = true;
                return true;
            }
            object value = GetControlValue(input);
            string textValue = "";
            if (value != null)
            {
                textValue = value.ToString();
            }
            if (_EmptyValueIsValid && string.IsNullOrEmpty(textValue))
            {
                this.LastValidationResult = true;
                return true;
            }
            Regex regularExpression = new Regex(_ValidationExpression);
            this.LastValidationResult = regularExpression.IsMatch(textValue);
            return LastValidationResult;
        }
        #endregion
    }
}
#endif