Example #1
0
        /// <summary>
        /// Perform Custom Validation on specific control.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private ValidationRule CustomValidate(Control ctrl)
        {
            ValidationRule vr = _ValidationRules[ctrl] as ValidationRule;

            if (vr != null)
            {
                CustomValidationEventArgs e = new CustomValidationEventArgs(ctrl.Text, vr);
                vr.OnCustomValidationMethod(e);
            }
            return(vr);
        }
Example #2
0
 /// <summary>
 /// Compare two values.
 /// </summary>
 /// <param name="leftText"></param>
 /// <param name="rightText"></param>
 /// <param name="op"></param>
 /// <param name="vr"></param>
 /// <returns></returns>
 public static bool Compare(string leftText,
                            string rightText,
                            ValidationCompareOperator op,
                            ValidationRule vr)
 {
     if (false == vr.IsCaseSensitive && vr.DataType == ValidationDataType.String)
     {
         leftText  = leftText.ToLower();
         rightText = rightText.ToLower();
     }
     return(ValidationUtil.CompareValues(leftText, rightText, op, vr.DataType));
 }
Example #3
0
        /// <summary>
        /// Perform RequiredField Validation on a specific control.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private ValidationRule RequiredFieldValidate(Control ctrl)
        {
            ValidationRule vr = _ValidationRules[ctrl] as ValidationRule;

            if (vr != null && vr.IsRequired)
            {
                ValidationRule vr2 = new ValidationRule();
                vr.IsValid = !ValidationRule.Compare(ctrl.Text, vr.InitialValue, ValidationCompareOperator.Equal, vr);
            }

            return(vr);
        }
Example #4
0
        /// <summary>
        /// Perform Custom Validation on specific control.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private ValidationRule CustomValidate(Control ctrl)
        {
            ValidationRule vr = _ValidationRules[ctrl] as ValidationRule;

            if (vr != null)
            {
                CustomValidationEventArgs e = new CustomValidationEventArgs(ctrl.Text, vr);
                vr.OnCustomValidationMethod(e);
                //add by rad 2011/8/22
                vr.IsCustomError = !vr.IsValid;
            }
            return(vr);
        }
Example #5
0
        /// <summary>
        /// Perform RequiredField Validation on a specific control.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private ValidationRule RequiredFieldValidate(Control ctrl)
        {
            ValidationRule vr = _ValidationRules[ctrl] as ValidationRule;

            if (vr != null && vr.IsRequired)
            {
                ValidationRule vr2 = new ValidationRule();
                vr.IsValid = !ValidationRule.Compare(this.GetValueFromCtrl(ctrl), vr.InitialValue, ValidationCompareOperator.Equal, vr);
                //add by rad 2011/8/22
                vr.IsRequiredFieldError = !vr.IsValid;
            }

            return(vr);
        }
Example #6
0
        /// <summary>
        /// Perform CompareValidate on a specific control.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns>true if control has no validation rule.</returns>
        private ValidationRule CompareValidate(Control ctrl)
        {
            ValidationRule vr = _ValidationRules[ctrl] as ValidationRule;

            if (vr != null)
            {
                if (this._DefaultValidationRule.ValueToCompare.Equals(vr.ValueToCompare) &&
                    this._DefaultValidationRule.Operator.Equals(vr.Operator))
                {
                    return(vr);
                }

                vr.IsValid = ValidationRule.Compare(ctrl.Text, vr.ValueToCompare, vr.Operator, vr);
            }

            return(vr);
        }
Example #7
0
        /// <summary>
        /// Perform validation on all controls.
        /// </summary>
        /// <returns>False if any control contains invalid data.</returns>
        public bool Validate()
        {
            bool           bIsValid = true;
            ValidationRule vr       = null;

            foreach (Control ctrl in _ValidationRules.Keys)
            {
                this.Validate(ctrl);

                vr = this.GetValidationRule(ctrl);
                if (vr != null && vr.IsValid == false)
                {
                    bIsValid = false;
                }
            }
            return(bIsValid);
        }
Example #8
0
        /// <summary>
        /// Perform validation on specific control.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private bool Validate(Control ctrl)
        {
            ValidationRule vr = this.GetValidationRule(ctrl);

            if (vr != null)
            {
                vr.ResultErrorMessage = string.Empty;
                vr.IsValid            = true;
            }

            if (vr == null || vr.IsValid)
            {
                vr = this.RequiredFieldValidate(ctrl);
            }

            if (vr == null || vr.IsValid)
            {
                vr = this.DataTypeValidate(ctrl);
            }

            if (vr == null || vr.IsValid)
            {
                vr = this.CompareValidate(ctrl);
            }

            if (vr == null || vr.IsValid)
            {
                vr = this.RangeValidate(ctrl);
            }

            if (vr == null || vr.IsValid)
            {
                vr = this.RegularExpressionValidate(ctrl);
            }


            if (vr == null || vr.IsValid)
            {
                vr = this.CustomValidate(ctrl);
            }

            return((vr == null) ? true : vr.IsValid);
        }
Example #9
0
        /// <summary>
        /// Set validation rule.
        /// </summary>
        /// <param name="inputComponent"></param>
        /// <param name="vr"></param>
        public void SetValidationRule(object inputComponent, ValidationRule vr)
        {
            if (inputComponent != null)
            {
                // only throw error in DesignMode
                if (base.DesignMode)
                {
                    if (!this.CanExtend(inputComponent))
                    {
                        throw new InvalidOperationException(inputComponent.GetType().ToString()
                                                            + " is not supported by the validation provider.");
                    }

                    if (!this.IsDefaultRange(vr) &&
                        ValidationRule.Compare(vr.MinimumValue, vr.MaximumValue, ValidationCompareOperator.GreaterThanEqual, vr))
                    {
                        throw new ArgumentException("MinimumValue must not be greater than or equal to MaximumValue.");
                    }
                }

                ValidationRule vrOld = this._ValidationRules[inputComponent] as ValidationRule;

                // if new rule is valid and in not DesignMode, clone rule
                if ((vr != null) && !base.DesignMode)
                {
                    vr = vr.Clone() as ValidationRule;
                }


                if (vr == null) // if new is null, no more validation
                {
                    this._ValidationRules.Remove(inputComponent);
                }
                else if (vrOld == null)
                {
                    this._ValidationRules.Add(inputComponent, vr);
                }
                else if ((vr != null) && (vrOld != null))
                {
                    this._ValidationRules[inputComponent] = vr;
                }
            }
        }
Example #10
0
        /// <summary>
        /// Perform Range Validation on specific control.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private ValidationRule RangeValidate(Control ctrl)
        {
            ValidationRule vr = _ValidationRules[ctrl] as ValidationRule;

            if (vr != null)
            {
                if (this.IsDefaultRange(vr))
                {
                    return(vr);
                }

                vr.IsValid = ValidationRule.Compare(ctrl.Text, vr.MinimumValue, ValidationCompareOperator.GreaterThanEqual, vr);

                if (vr.IsValid)
                {
                    vr.IsValid = ValidationRule.Compare(ctrl.Text, vr.MaximumValue, ValidationCompareOperator.LessThanEqual, vr);
                }
            }
            return(vr);
        }
Example #11
0
        public void Check_Init()
        {
            ValidationRule ruleUserId = new ValidationRule();

            ruleUserId.IsRequired = true;

            ruleUserId.RequiredFieldErroMessage = MessageUtils.GetMessage("W0001", this.lblUserId.Text);
            validationProvider1.SetValidationRule(this.txtUserId, ruleUserId);

            ValidationRule rulePassword = new ValidationRule();

            rulePassword.IsRequired = true;
            rulePassword.RequiredFieldErroMessage = MessageUtils.GetMessage("W0001", this.lblPassword.Text);
            validationProvider1.SetValidationRule(this.txtPassword, rulePassword);

            Noogen.Validation.ValidationRule ruleLoginCheck = new Noogen.Validation.ValidationRule();
            ruleLoginCheck.CustomValidationMethod +=
                new Noogen.Validation.CustomValidationEventHandler(vr_CustomLoginValidationMethod);
            ruleLoginCheck.CustomErrorMessage = MessageUtils.GetMessage("W0003");
            validationProvider2.SetValidationRule(this.txtUserId, ruleLoginCheck);
        }
Example #12
0
        /// <summary>
        /// Validate Data Type.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private ValidationRule DataTypeValidate(Control ctrl)
        {
            ValidationRule vr = this._ValidationRules[ctrl] as ValidationRule;

            if (vr != null && vr.Operator.Equals(ValidationCompareOperator.DataTypeCheck))
            {
                if (vr.DataType.Equals(this._DefaultValidationRule.DataType))
                {
                    return(vr);
                }

                System.Web.UI.WebControls.ValidationDataType vdt =
                    (System.Web.UI.WebControls.ValidationDataType)Enum.Parse(
                        typeof(System.Web.UI.WebControls.ValidationDataType),
                        vr.DataType.ToString());

                vr.IsValid = ValidationUtil.CanConvert(ctrl.Text, vdt);
            }

            return(vr);
        }
Example #13
0
        /// <summary>
        /// Perform Regular Expression Validation on a specific control.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private ValidationRule RegularExpressionValidate(Control ctrl)
        {
            ValidationRule vr = _ValidationRules[ctrl] as ValidationRule;

            if (vr != null)
            {
                try
                {
                    if (this._DefaultValidationRule.RegExPattern.Equals(vr.RegExPattern))
                    {
                        return(vr);
                    }

                    vr.IsValid = ValidationUtil.ValidateRegEx(ctrl.Text, vr.RegExPattern);
                }
                catch (Exception ex)
                {
                    vr.ResultErrorMessage = "RegEx Validation Exception: " + ex.Message + Environment.NewLine;
                    vr.IsValid            = false;
                }
            }
            return(vr);
        }
        /// <summary>
        /// Perform RequiredField Validation on a specific control.
        /// </summary>
        /// <param name="ctrl"></param>
        /// <returns></returns>
        private ValidationRule RequiredFieldValidate(Control ctrl)
        {
            ValidationRule vr = _ValidationRules[ctrl] as ValidationRule;

            if (vr != null && vr.IsRequired)
            {
                ValidationRule vr2 = new ValidationRule();
                vr.IsValid = !ValidationRule.Compare(ctrl.Text, vr.InitialValue, ValidationCompareOperator.Equal, vr);
            }

            return vr;
        }
		/// <summary>
		/// Compare two values.
		/// </summary>
		/// <param name="leftText"></param>
		/// <param name="rightText"></param>
		/// <param name="op"></param>
		/// <param name="vr"></param>
		/// <returns></returns>
		public static bool Compare(	string leftText, 
									string rightText, 
									ValidationCompareOperator op, 
									ValidationRule vr)
		{
			if (false == vr.IsCaseSensitive && vr.DataType == ValidationDataType.String)
			{
				leftText = leftText.ToLower();
				rightText = rightText.ToLower();
			}
			return ValidationUtil.CompareValues(leftText, rightText, op, vr.DataType);
		}
Example #16
0
        /// <summary>
        /// Get validation error messages.
        /// Modified by rad 2011/08/22
        /// return IList<MessageVo>
        /// </summary>
        public IList <MessageVo> ValidationMessages(bool showErrorIcon)
        {
            ValidationRule    vr          = null;
            IList <MessageVo> messagelist = new List <MessageVo>();

            foreach (Control ctrl in _ValidationRules.Keys)
            {
                vr = this.GetValidationRule(ctrl);

                if (vr != null)
                {
                    if (vr.IsValid == false)
                    {
                        MessageVo msgvo = new MessageVo();
                        msgvo.MessageType = "Warning";
                        if (vr.IsRequiredFieldError)
                        {
                            vr.ResultErrorMessage += vr.RequiredFieldErroMessage;
                            msgvo.MessageType      = "Warning";
                        }
                        if (vr.IsDataTypeError)
                        {
                            vr.ResultErrorMessage += vr.DataTypeErrorMessage;
                            msgvo.MessageType      = "Warning";
                        }
                        if (vr.IsCompareError)
                        {
                            vr.ResultErrorMessage += vr.CompareErrorMessage;
                            msgvo.MessageType      = "Warning";
                        }
                        if (vr.IsRangeError)
                        {
                            vr.ResultErrorMessage += vr.RangeErroMessage;
                            msgvo.MessageType      = "Warning";
                        }
                        if (vr.IsRegularExpressionError)
                        {
                            vr.ResultErrorMessage += vr.RegularExpressionErrorMessage;
                            msgvo.MessageType      = "Warning";
                        }
                        if (vr.IsCustomError)
                        {
                            vr.ResultErrorMessage += vr.CustomErrorMessage;
                            msgvo.MessageType      = "Warning";
                        }
                        vr.ResultErrorMessage = vr.ResultErrorMessage.Replace("%ControlName%", ctrl.Name);
                        msgvo.ResultMessage   = vr.ResultErrorMessage;
                        messagelist.Add(msgvo);
                    }
                    if (showErrorIcon)
                    {
                        this._ErrorProvider.SetError(ctrl, vr.ResultErrorMessage);
                    }
                    else
                    {
                        this._ErrorProvider.SetError(ctrl, null);
                    }
                }
            }
            return(messagelist);
        }
Example #17
0
 /// <summary>
 /// Check if validation rule range is default.
 /// </summary>
 /// <param name="vr"></param>
 /// <returns></returns>
 private bool IsDefaultRange(ValidationRule vr)
 {
     return(this._DefaultValidationRule.MinimumValue.Equals(vr.MinimumValue) &&
            this._DefaultValidationRule.MaximumValue.Equals(vr.MaximumValue));
 }
 /// <summary>
 /// Default Ctor.
 /// </summary>
 /// <param name="Value"></param>
 /// <param name="vr"></param>
 public CustomValidationEventArgs(object Value, ValidationRule vr)
 {
     this._Value          = Value;
     this._ValidationRule = vr;
 }
		/// <summary>
		/// Default Ctor.
		/// </summary>
		/// <param name="Value"></param>
		/// <param name="vr"></param>
		public CustomValidationEventArgs(object Value, ValidationRule vr)
		{
			this._Value = Value;
			this._ValidationRule = vr;
		}
        /// <summary>
        /// Check if validation rule range is default.
        /// </summary>
        /// <param name="vr"></param>
        /// <returns></returns>
        private bool IsDefaultRange(ValidationRule vr)
        {
            return (this._DefaultValidationRule.MinimumValue.Equals(vr.MinimumValue)
                && this._DefaultValidationRule.MaximumValue.Equals(vr.MaximumValue));

        }
        /// <summary>
        /// Set validation rule.
        /// </summary>
        /// <param name="inputComponent"></param>
        /// <param name="vr"></param>
        public void SetValidationRule(object inputComponent, ValidationRule vr)
        {
            if (inputComponent != null)
            {
                // only throw error in DesignMode
                if (base.DesignMode)
                {
                    if (!this.CanExtend(inputComponent))
                        throw new InvalidOperationException(inputComponent.GetType().ToString()
                            + " is not supported by the validation provider.");

                    if (!this.IsDefaultRange(vr)
                        && ValidationRule.Compare(vr.MinimumValue, vr.MaximumValue, ValidationCompareOperator.GreaterThanEqual, vr))
                        throw new ArgumentException("MinimumValue must not be greater than or equal to MaximumValue.");
                }

                ValidationRule vrOld = this._ValidationRules[inputComponent] as ValidationRule;

                // if new rule is valid and in not DesignMode, clone rule
                if ((vr != null) && !base.DesignMode)
                {
                    vr = vr.Clone() as ValidationRule;
                }


                if (vr == null)	// if new is null, no more validation
                {
                    this._ValidationRules.Remove(inputComponent);
                }
                else if (vrOld == null)
                {
                    this._ValidationRules.Add(inputComponent, vr);
                }
                else if ((vr != null) && (vrOld != null))
                {
                    this._ValidationRules[inputComponent] = vr;
                }

            }
        }