ASP.Net validation for number of days between Arrival & Departure days
I have a requirement by which need to check validation between number of days entered between two date selectors [From & To Dates]. My requirement is that it should not exceed 100 days.
Is there a way I can do with asp.net provided validators. I can go ahead and write customvalidator for it (both client and server side), but wondering if that is doable using CompareValidator or RangeValida开发者_Python百科tor?
Try using custom validator:
<asp:CustomValidator ID="valCustmCheckDate" runat="server" ErrorMessage="The date difference should not be greater than 100 days" ForeColor="Red" ValidationGroup="LoginUserAdd" ClientValidationFunction="CompareStartAndEndDate"></asp:CustomValidator>
Call the following function in javascript:
function CompareStartAndEndDate(sender,args) {
var txtFromExpiryDate = document.getElementById('<%=txtFromDate.ClientID %>');//dd/mm/yyyy format
var txtToExpiryDate = document.getElementById('<%=txtToDate.ClientID %>');//dd/mm/yyyy format
var a = txtFromDate.value.split('/');
var b = txtToDate.value.split('/');
var FromDate = new Date(a[2], a[1] - 1, a[0]);
var ToDate = new Date(b[2], b[1] - 1, b[0]);
var newFromDate =FromDate.getTime();
var newToDate=ToDate.getTime();
var dateDiffInMilliseconds= newToDate-newFromDate;
var dateDiffInDays=dateDiffInMilliseconds/(1000 * 60 * 60 * 24)
if (dateDiffInDays>100 ) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
Hope this will do it for you...
Below function will do the work if you are looking after similar kinda answer
function CheckDateRange(start, end, numberOfDays) {
// Parse the entries
var startDate = Date.parse(start);
var endDate = Date.parse(end);
// Make sure they are valid
if (isNaN(startDate)) {
alert("The start date provided is not valid, please enter a valid date.");
return false;
}
if (isNaN(endDate)) {
alert("The end date provided is not valid, please enter a valid date.");
return false;
}
// Check the date range, 86400000 is the number of milliseconds in one day
var difference = (endDate - startDate) / (86400000 * numberOfDays);
if (difference < 0) {
alert("The start date must come before the end date.");
return false;
}
if (difference >= 1) {
alert("The range must not exceed 100 days.");
return false;
}
return true;
}
Got help from somewhat similar post
精彩评论