If the button is a submit button, then add this to the onclick(). A true return value will allow the page to continue submitting, and a false will stop the postback (submit).
EDIT: Just try it before you say that its not right. It looks complicated but its pretty simple. Get the first date, Get the second date, compare them. If there is less than 3 weeks diff, return true and allow the page to post back (submit), else alert the user and return thier answer.
The function...
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;
}