2015-10-05 55 views
1

我在我的項目中使用了kendo日期選擇器。 Kendo支持用戶編輯日期,並允許自由文本。如何防止用戶輸入除日期以外的任何內容。Kendo datepicker不可編輯

+1

http://www.telerik.com/forums/input-mask-112a84d01802 –

回答

1

你有兩個我知道解決這個問題的選項。

選項1:

創建一個自定義的驗證: http://docs.telerik.com/kendo-ui/framework/validator/overview

選項2:

在保存或數據的變化,你可以檢查它的日期是真正有效的(我在我自己的實用工具類包裹這讓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; 
};