This question is locked. New answers and comments are not allowed.
Dear Telerik Team,
I've got a model validation IsDateAfter like
For client side, i've got a file customvalidation.js
My model is
I'm using telerik datepickerfor extersion for decommissioning date like
I've got all the files for unobtrusive calls like jquery.unobtrusive-ajax.min.js, jquery.validate.min.js, jquery.validate.unobtrusive.min.js and customvalidation.js in my page, but it is not firing the client side validation.
Please let me know what I'm doing wrong here.
Thank you
I've got a model validation IsDateAfter like
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = false)] public sealed class IsDateAfter : ValidationAttribute, IClientValidatable { private readonly string dateToCompare; private readonly bool allowEqualDates; public IsDateAfter(string dateToCompare, bool allowEqualDates = false) { this.dateToCompare = dateToCompare; this.allowEqualDates = allowEqualDates; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var propertyTestedInfo = validationContext.ObjectType.GetProperty(this.dateToCompare); if (propertyTestedInfo == null) { return new ValidationResult(string.Format("unknown property {0}", this.dateToCompare)); } var propertyTestedValue = propertyTestedInfo.GetValue(validationContext.ObjectInstance, null); if (propertyTestedValue != null) { if (!(propertyTestedValue is DateTime)) { propertyTestedValue = DateTime.ParseExact(propertyTestedValue.ToString(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None); } } if (value == null || !(value is DateTime)) { return ValidationResult.Success; } if (propertyTestedValue == null || !(propertyTestedValue is DateTime)) { return ValidationResult.Success; } //compare values if ((DateTime)value >= (DateTime)propertyTestedValue) { if (this.allowEqualDates) { return ValidationResult.Success; } if ((DateTime)value > (DateTime)propertyTestedValue) { return ValidationResult.Success; } } return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); } public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context) { var rule = new ModelClientValidationRule { ErrorMessage = this.ErrorMessageString, ValidationType = "isdateafter" }; rule.ValidationParameters["datetocompare"] = this.dateToCompare; rule.ValidationParameters["allowequaldates"] = this.allowEqualDates; yield return rule; } }For client side, i've got a file customvalidation.js
//date validator start $.validator.unobtrusive.adapters.add( 'isdateafter', ['datetocompare', 'allowequaldates'], function (options) { options.rules['isdateafter'] = options.params; options.messages['isdateafter'] = options.message; }); $.validator.addMethod("isdateafter", function (value, element, params) { var parts = element.name.split("."); var prefix = ""; if (parts.length > 1) prefix = parts[0] + "."; var startdatevalue = $('input[name="' + prefix + params.datetocompare + '"]').val(); if (!value || !startdatevalue) return true; var startSplit = startdatevalue.split('/'); var newStartDateValue = new Date(startSplit[2], startSplit[1], startSplit[0]); var startTimeStamp = newStartDateValue.getTime(); var valueSplit = value.split('/'); var newValue = new Date(valueSplit[2], valueSplit[1], valueSplit[0]); var valueTimeStamp = newValue.getTime(); return (params.allowequaldates) ? startTimeStamp <= valueTimeStamp : startTimeStamp < valueTimeStamp; }); //date validator endMy model is
public class FleetDecommissionModel { [ScaffoldColumn(false)] public int FLEET_ID { get; set; } [Display(Name = "Delivery date:")] public string DELIVERY_DATE { get; set; } [Display(Name = "Decommissioning date:")] [Required] [IsDateAfter("DELIVERY_DATE", false, ErrorMessage="Date must not be before delivery date")] public DateTime? DECOMMISSIONING_DATE { get; set; } }I'm using telerik datepickerfor extersion for decommissioning date like
@(Html.Telerik().DatePickerFor(f=>f.DECOMMISSIONING_DATE) .HtmlAttributes(new { id = "DatePicker_wrapper" }) .Format("dd/MM/yyyy") )I've got all the files for unobtrusive calls like jquery.unobtrusive-ajax.min.js, jquery.validate.min.js, jquery.validate.unobtrusive.min.js and customvalidation.js in my page, but it is not firing the client side validation.
Please let me know what I'm doing wrong here.
Thank you