2017-04-26 98 views
0

我在自定義列表中有一個日期選擇器字段(SharePoint 2013)。我正在嘗試對特定的日期字段(DtStartDate)進行驗證。 條件是: 1)用戶不應該從當前日期選擇過去的日期 2)用戶應該從當前日期的下個月選擇日期。例如:如果日期爲2017年4月26日,則用戶不應選擇任何日期至2017年5月26日。如果他們選擇,我想顯示一條錯誤消息。驗證日期 - 用戶應只選擇下個月的日期

我曾嘗試下面的代碼:

var dtStartDate = $("input[title='Start Date Required Field']").val(); 
// Get today's date 
var todaysDate = new Date(); 
// Create date from input value 
if(dtStartDate.length > 0) 
{     
inputStartDate = new Date(dtStartDate); 
// call setHours to take the time out of the comparison 
if((inputStartDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) || (inputStartDate < todaysDate)) { 
// StartDate equals today's date or Date is in the Past 
msg = msg + "<br/> Demand Start Date should be greater than Today's Date"; 
} 
} 

如何檢查確認爲第二種情況? 感謝

回答

0

的Tru添加天的日期選擇器

Datefrom.MinDate = DateTime.Now.AddDays(60); 
0

希望這將有助於

var d = new Date();  // current date 
 
var date = d.setMonth(d.getMonth() + 1); // add month to the current date 
 
var inputDate = new Date(d.setMonth(d.getMonth() + 2)); // add two months to the current date 
 
console.log('comparison date - ' + new Date(date)); 
 
console.log('input date - ' + inputDate); 
 
if(date < inputDate.getTime()) 
 
    console.log('valid date'); 
 
else 
 
    console.log('invalid date');

+0

嗨。我嘗試了代碼。我收到錯誤爲「對象不支持屬性或方法'getMonth'」 – venkat14

+0

您可以發佈已經過測試的腳本。使用getMonth方法對象應該是一個日期對象。 var d = new Date(); d.getMonth() –