Kendo datepicker不可编辑

时间:2015-10-05 17:55:52

标签: javascript kendo-ui kendo-datepicker

我在我的项目中使用了剑道日期选择器。 Kendo支持用户编辑日期,它也允许自由文本。如何防止用户输入除日期之外的任何内容。

1 个答案:

答案 0 :(得分:1)

我知道有两个方法可以解决这个问题。

选项1:

创建自定义验证器: http://docs.telerik.com/kendo-ui/framework/validator/overview

选项2:

在保存OR数据更改时,您可以检查日期是否真正有效(我已将其包装在我自己的实用程序类中,以便JQuery不在我的视图模型中):

function isValidDateTimePickerDate(id, allowNullsOrBlank) {
/// <summary>Examines the date in a Telerik date time picker to see if it is valid.</summary>
/// <param name="id" type="string">A string that represents the element's id (assumes it is prefixed with # -- my utility does not assume this, but I've condensed it for this post)</param>
/// <param name="allowNullsOrBlank" type="boolean">Indicates if a null is valid</param>
if (allowNullsOrBlank == null) {
    allowNullsOrBlank = true;
}

var value = $(id).val();

// Allow the user to have null or blank?
if (value == null || value.length === 0) {
    return allowNullsOrBlank;
}

var date = kendo.parseDate(value);
if (!date) {
    return false;
}

// Dates prior to 1900 are not valid and dates lower than 1753 
// will throw database exceptions in SQL Server
if (date.getFullYear() < 1900) {
    return false;
}

return true;
};