Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
740
Validating UltraDateTimeEditor Field
posted

Hi there,

I want to validate Date of Birth field which uses a UltraDateTimeEditor with following conditions:

1. If the user selects a Date of Birth greater than TODAY's date, shoud give an error "The date of birth must be before today"

2. If the user selects a date, greater than 2 months before  e.g. Today  - 22-10-2008 :Selected Date - 22-07-2008, should give a warning : "Warning : xxxxx!!" 3. For the both of the condition falis, default the UltraDateTimeEditor text in to "00/00/0000" format and focusI would like to know whether I can do both these validations using the Validation Settings in the ultravalidator or do I have to do it in the coding 

I have written the following coding, but it doesn’t give the outputs that I needed. 

=================================================================================================

 // For the 1st Conditionthis.udtDOB.Name = "udtDOB";

this.udtDOB.NullText = "00/00/0000";

this.udtDOB.Size = new System.Drawing.Size(95, 21);

this.udtDOB.TabIndex = 24;

ultraToolTipInfo2.ToolTipText = "Please enter date of birth";

this.ultraToolTipManager1.SetUltraToolTip(this.udtDOB, ultraToolTipInfo2);

this.ultraValidator1.GetValidationSettings(this.udtDOB).Condition = new Infragistics.Win.OperatorCondition(Infragistics.Win.ConditionOperator.GreaterThan, System.DateTime.Today, true, typeof(System.DateTime));

this.ultraValidator1.GetValidationSettings(this.udtDOB).DataType = typeof(System.DateTime);

this.ultraValidator1.GetValidationSettings(this.udtDOB).IsRequired = true;

this.ultraValidator1.GetValidationSettings(this.udtDOB).NotificationSettings.Caption = "Validation Faild";

this.ultraValidator1.GetValidationSettings(this.udtDOB).NotificationSettings.Text = "The date of birth must be before today";

this.ultraValidator1.GetValidationSettings(this.udtDOB).ValidationPropertyName = "Value";

this.ultraValidator1.GetValidationSettings(this.udtDOB).ValidationTrigger = Infragistics.Win.Misc.ValidationTrigger.OnValidating;

this.udtDOB.Value = null;

=================================================================================================

// For the 2nd Condition

        private void udtDOB_ValueChanged(object sender, EventArgs e)        {                  if (udtDOB.Value != null)           {                int monthDiff = DateTime.Today.Month - Convert.ToDateTime(udtDOB.Value).Month;                 if (monthDiff > 2)                {                    MessageBox.Show(this, "Warning xxxxxxxxxxx", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);                    udtDOB.Value = null;                }            }        } 

=================================================================================================

Urgent Help needed ! Thanks in Advance!

NW 

 

Parents
  • 69832
    Offline posted

    The short answer to your question is that you will have to write code to do this. You could theoretically use two UltraValidators, one for a date range of two months ago through the current date, and another for the current date and later (the form'er error message would be a "warning" and the latter's an error), but I don't ever recommend using two validators on the same control.

    The following code sample demonstrates how to use the ValidationError event to change the error message that is displayed to the end user based on how "far" the validated value is from some arbitrarily defined value, in your case, 2 months before the current date:

    this.ultraDateTimeEditor1.NullText = "00/00/0000";
    this.ultraDateTimeEditor1.Value = null;
    ValidationSettings vs = this.validator.GetValidationSettings(this.ultraDateTimeEditor1);
    vs.ValidationPropertyName = "Value";
    DateTime maxDOB = DateTime.Today.AddMonths( -2 );
    vs.Condition = new DOBCondition( maxDOB );

    public class DOBCondition : RangeCondition
    {
        public DOBCondition( DateTime maxDOB ) : base(DateTime.MinValue, maxDOB, typeof(DateTime) ){}

        public enum WarningType
        {
            None,
            Warning,
            Error
        }

        public WarningType GetWarningType( DateTime value )
        {
            DateTime maxDOB = (DateTime)this.MaximumValue;
            if ( value > DateTime.Today )
                return WarningType.Error;
            else
            if ( value > maxDOB )
                return WarningType.Warning;
            else
                return WarningType.None;
        }
    }


    private void validator_ValidationError(object sender, Infragistics.Win.Misc.ValidationErrorEventArgs e)
    {
        ValidationResult result = e.Validation.Errors[0];
        ValidationSettings vs = e.Validation.Errors[0].ValidationSettings;
        UltraDateTimeEditor dateTimeEditor = e.Control as UltraDateTimeEditor;
        if ( dateTimeEditor != null && vs.Condition is DOBCondition )
        {
            DOBCondition condition = vs.Condition as DOBCondition;
            DOBCondition.WarningType warning = condition.GetWarningType( (DateTime)result.Value );

            e.NotificationSettings.Caption = warning.ToString();

            switch ( warning )
            {
                case DOBCondition.WarningType.Warning:
                    e.NotificationSettings.Text = "Warning : xxxxx!!";
                    break;

                case DOBCondition.WarningType.Error:
                    e.NotificationSettings.Text = "The date of birth must be before today";
                    break;
            }
        }
    }

Reply Children