kbv*_*hnu 2 javascript validation jquery datetime
I am having a start time,start date and end date,end time on my web page.I need to ensure that if the start date and end date is equal then the end time must be greater than start time and if end date is greater than start date then user can able to enter any time.How can I achieve this validation?
// get the values from your form
var startDate = $('input[name=startDate]').val(),
endDate = $('input[name=endDate]').val(),
startTime = $('input[name=startTime]').val(),
endTime = $('input[name=endTime]').val()
Run Code Online (Sandbox Code Playgroud)
如果您决定使用Datejs,则应使用输入字段解析值Date.parse(startDate).查看入门指南.如果您决定不使用DateJ,则可以将getTime()附加到startTime var.(例如 - endTime.getTime() < startTime.getTime())
var error = '';
// if the start date is after the end date
if (startDate > endDate){
var error = error + 'Your start date is after your end date.';
}
// if the start date and end date are the same -- and end time is before the start time
else if(startDate == endDate && endTime < startTime){
var error = error + 'Your end time is before your start time.';
};
// handle any errors
if(error.length){
alert(error);
};
Run Code Online (Sandbox Code Playgroud)