예제 #1
0
        /// <summary>
        /// We only validate that to is less than from. There is no need to validate that from is greter than to.
        /// </summary>
        /// <param name="ctlInput"></param>
        /// <exception cref="NotImplementedException"></exception>
        /// <returns></returns>
        protected override bool OnServerValidate(InputControlBase ctlInput)
        {
            // We only expect to be associated with a text box
            TextBoxEx tb = (TextBoxEx)ctlInput;

            if (string.IsNullOrEmpty(tb.Text))
            {
                return(true);        // Empty values are valid
            }

            DateTime?value = tb.ValueAsDate;

            if (!value.HasValue)
            {
                ctlInput.ErrorMessage = string.Format(MSG1_VALID, ctlInput.FriendlyName);
                ctlInput.IsValid      = false;
                return(false);
            }

            switch (this.DateType)
            {
            case DateType.Default:
                break;

            case DateType.ToDate:
                // Get the previous TextBoxEx
                TextBoxEx tbFrom   = GetFromControl(ctlInput);
                DateTime? dateFrom = tbFrom.ValueAsDate;
                if (dateFrom.HasValue)
                {
                    if (value < dateFrom)
                    {
                        ctlInput.ErrorMessage = string.Format(MSG2_TO_GE_FROM, ctlInput.FriendlyName, tbFrom.FriendlyName);
                        ctlInput.IsValid      = false;
                        return(false);
                    }
                    if (this.MaxRange > 0 && (value.Value - dateFrom.Value).TotalDays > this.MaxRange)
                    {
                        ctlInput.IsValid = false;
                        return(false);
                    }
                }
                break;

            default:
                throw new NotImplementedException();
            }

            if (this.Min.HasValue)
            {
                DateTime minDate = DateTime.Today + TimeSpan.FromDays(this.Min.Value);
                if (value < minDate)
                {
                    ctlInput.ErrorMessage = string.Format(CultureInfo.CurrentUICulture, MSG2_MINDATE, ctlInput.FriendlyName, minDate);
                    ctlInput.IsValid      = false;
                    return(false);
                }
            }
            if (this.Max.HasValue)
            {
                DateTime maxDate = DateTime.Today + TimeSpan.FromDays(this.Max.Value);
                if (value > maxDate)
                {
                    ctlInput.ErrorMessage = string.Format(CultureInfo.CurrentUICulture, MSG2_MAX, ctlInput.FriendlyName, maxDate);
                    ctlInput.IsValid      = false;
                    return(false);
                }
            }
            return(true);
        }
예제 #2
0
        /// <summary>
        /// Attach a datepicker with the textbox.
        /// </summary>
        /// <param name="dateValidator"></param>
        private void RegisterDatePicker(Date dateValidator)
        {
            DatePickerOptions.Add("showOn", "button");
            if (dateValidator.Min.HasValue)
            {
                DatePickerOptions.Add("minDate", dateValidator.Min.Value);
            }
            if (dateValidator.Max.HasValue)
            {
                DatePickerOptions.Add("maxDate", dateValidator.Max.Value);
            }
            DatePickerOptions.Add("showOtherMonths", true);
            DatePickerOptions.Add("changeMonth", true);
            //DatePickerOptions.Add("buttonText", "<SPAN class=\"ui-button-icon-only ui-icon ui-icon-calendar\"></SPAN>");
            // Because of the possibility of watermark, we must use this
            // event to set the value. To help DropDownSuggest, we are raising the keypress event.
            DatePickerOptions.AddRaw("onSelect", @"function(dateText, inst) {
$(this).val(dateText).change();
}");

            // The date should use the current UI culture
            if (!this.DatePickerOptions.ContainsKey("dateFormat"))
            {
                switch (CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern)
                {
                case "M/d/yyyy":
                case "M'/'d'/'yyyy":
                    DatePickerOptions.Add("dateFormat", "m/d/yy");
                    break;

                case "d/M/yyyy":
                    DatePickerOptions.Add("dateFormat", "d/m/yy");
                    break;

                case "dd-MMM-yy":
                    DatePickerOptions.Add("dateFormat", "d-M-y");
                    break;

                case "MM/dd/yyyy":
                case "MM'/'dd'/'yyyy":
                    DatePickerOptions.Add("dateFormat", "mm/dd/yy");
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
            this.ClientOptions.Add("pickerOptions", DatePickerOptions);
            switch (dateValidator.DateType)
            {
            case DateType.Default:
                break;

            case DateType.ToDate:
                TextBoxEx tbFrom = dateValidator.GetFromControl(this);
                this.ClientOptions.Add("fromSelector", tbFrom.ClientSelector);
                if (dateValidator.MaxRange > 0)
                {
                    this.ClientOptions.Add("maxDateRange", dateValidator.MaxRange);
                }
                break;

            default:
                break;
            }
        }
예제 #3
0
        /// <summary>
        /// Add client rules for main and max values. Also add a compare validator to the to date
        /// </summary>
        /// <param name="ctlInputControl"></param>
        /// <exception cref="NotImplementedException"></exception>
        internal override void RegisterClientRules(InputControlBase ctlInputControl)
        {
            string param;

            // date picker utility function provides robust date validation
            string script  = @"
if (this.optional(element)) return true;
var fmt = $(element).datepicker('option', 'dateFormat');
try {
    var dt = $.datepicker.parseDate(fmt, value);
    return true;
}
catch (err) {
    return false;
}
";
            string newRule = "$.validator.addMethod('checkdate', function(value, element, param) {"
                             + script + "});";

            JQueryScriptManager.Current.RegisterScriptBlock("checkdate_validator", newRule);
            string msg = string.Format(MSG1_VALID, ctlInputControl.FriendlyName);

            DoRegisterValidator(ctlInputControl, "checkdate", true, msg);

            if (this.Min.HasValue)
            {
                script  = @"
if (this.optional(element)) return true;
try {
    var fmt = $(element).datepicker('option', 'dateFormat');
    var dt = $.datepicker.parseDate(fmt, value);
    var minDate = $.datepicker.parseDate(fmt, param);
    return dt >= minDate;
}
catch (err) {
    // If any of the dates is not parseable, we deem ourselves to be valid
    return true;
}
";
                newRule = "$.validator.addMethod('mindate', function(value, element, param) {"
                          + script + "});";
                JQueryScriptManager.Current.RegisterScriptBlock("mindate_validator", newRule);
                DateTime minDate = DateTime.Today + TimeSpan.FromDays(this.Min.Value);
                param = string.Format(CultureInfo.CurrentUICulture, "{0:d}", minDate);
                msg   = string.Format(CultureInfo.CurrentUICulture, MSG2_MINDATE, ctlInputControl.FriendlyName, minDate);
                DoRegisterValidator(ctlInputControl, "mindate", param, msg);
            }

            if (this.Max.HasValue)
            {
                script  = @"
if (this.optional(element)) return true;
try {
    var fmt = $(element).datepicker('option', 'dateFormat');
    var dt = $.datepicker.parseDate(fmt, value);
    var maxDate = $.datepicker.parseDate(fmt, param);
    return dt <= maxDate;
}
catch (err) {
    return true;
}
";
                newRule = "$.validator.addMethod('maxdate', function(value, element, param) {"
                          + script + "});";
                JQueryScriptManager.Current.RegisterScriptBlock("maxdate_validator", newRule);
                DateTime maxdate = DateTime.Today + TimeSpan.FromDays(this.Max.Value);
                param = string.Format(CultureInfo.CurrentUICulture, "{0:d}", maxdate);
                msg   = string.Format(CultureInfo.CurrentUICulture, MSG2_MAX, ctlInputControl.FriendlyName, maxdate);
                DoRegisterValidator(ctlInputControl, "maxdate", param, msg);
            }
            switch (this.DateType)
            {
            case DateType.Default:
                break;

            case DateType.ToDate:
                // Ensure to date is more than from date
                script = @"
if (this.optional(element)) return true;
try {
    var fmt = $(element).datepicker('option', 'dateFormat');
    var val = $.datepicker.parseDate(fmt, value);
    var from = $.datepicker.parseDate(fmt, $(param).textBoxEx('getval'));
    if (from == '') return true;
    var fromDate = new Date(from);
    return val >= fromDate;
}
catch (err) {
    return true;
}
";
                // Creating a new rule which ensures to date is greater than from date
                newRule = "$.validator.addMethod('todate', function(value, element, param) {"
                          + script + "});";
                JQueryScriptManager.Current.RegisterScriptBlock("todate_validator", newRule);
                TextBoxEx tbFrom = GetFromControl(ctlInputControl);
                param = tbFrom.ClientSelector;
                msg   = string.Format(MSG2_TO_GE_FROM, ctlInputControl.FriendlyName, tbFrom.FriendlyName);
                DoRegisterValidator(ctlInputControl, "todate", param, msg);

                if (this.MaxRange > 0)
                {
                    // getDate can return a garbage date if any input control contains an invalid date.
                    // This is OK since the checkdate validator will fail first.
                    newRule = @"$.validator.addMethod('maxdaterange', function(value, element, param) {
var val = $(element).datepicker('getDate');
var fromDate = $(param[0]).datepicker('getDate');
return !val || !fromDate || (val.getTime() - fromDate.getTime()) <= (param[1] * 1000*60*60*24);
                        });";
                    JQueryScriptManager.Current.RegisterScriptBlock("maxdaterange_validator", newRule);
                    object[] ruleParams = new object[2];
                    ruleParams[0] = tbFrom.ClientSelector;
                    ruleParams[1] = string.Format("{0}", this.MaxRange);
                    msg           = string.Format("{0} must be within {1} days of {2}",
                                                  ctlInputControl.FriendlyName, this.MaxRange, tbFrom.FriendlyName);
                    DoRegisterValidator(ctlInputControl, "maxdaterange", ruleParams, msg);
                }
                break;

            default:
                throw new NotImplementedException();
            }
            //RegisterDatePicker(ctlInputControl);
        }