Если кнопка является кнопкой отправки, добавьте ее в onclick (). Истинное возвращаемое значение позволит странице продолжить отправку, а ложное остановит обратную передачу (отправка).
РЕДАКТИРОВАТЬ: Просто попробуйте, прежде чем сказать, что это не правильно. Это выглядит сложно, но довольно просто. Получить первое свидание, получить второе свидание, сравнить их. Если разница меньше 3 недель, верните true и разрешите странице отправлять обратно (отправить), иначе предупредите пользователя и верните свой ответ.
Функция ...
function warning() {
var ele;
var startDate;
var endDate;
var threeWeeksInMilliseconds = 1814400000; //1000 ms * 60 sec * 60 min * 24 hr * 21 days
//get starting value
ele = document.getElementById('txtStartDate');
if (ele == 'undefined'){
return false; //no start element
}
else {
try{
startDate = new Date(ele.value);
}
catch (e) {
return false;
}
}
//get the ending value
ele = document.getElementById('txtEndDate');
if (ele == 'undefined'){
return false; //no start element
}
else {
try{
endDate = new Date(ele.value);
}
catch (e) {
return false;
}
}
//getTime() returns milliseconds
if ((endDate.getTime() - startDate.getTime()) < threeWeeksInMilliseconds) {
return true;
}
//else present the message for confirmation.
var msg = "The date range you have selected will return a substantial " + "" +
"amount of data and will take some time to process.\n\n" +
"Are you sure you want to continue?";
var answer;
answer = confirm(msg);
if (answer) {
return true;
}
else {
return false;
}
//default return condition - nothing should get here so this indicates an error.
//Use true if you want to allow this to process.
return false;
}