How do I check/validate in jQuery whether end date [textbox] is greater than start date [textbox]?
How do I check/validate in jQuery whether end date [textbox] is greater than start date [textbox]?
Just expanding off fusions answer. this extension method works using the jQuery validate plugin. It will validate dates and numbers
jQuery.validator.addMethod("greaterThan",
function(value, element, params) {
if (!/Invalid|NaN/.test(new Date(value))) {
return new Date(value) > new Date($(params).val());
}
return isNaN(value) && isNaN($(params).val())
|| (Number(value) > Number($(params).val()));
},'Must be greater than {0}.');
To use it:
$("#EndDate").rules('add', { greaterThan: "#StartDate" });
or
$("form").validate({
rules: {
EndDate: { greaterThan: "#StartDate" }
}
});
var startDate = new Date($('#startDate').val());
var endDate = new Date($('#endDate').val());
if (startDate < endDate){
// Do something
}
That should do it I think
Reference jquery.validate.js and jquery-1.2.6.js. Add a startDate class to your start date textbox. Add an endDate class to your end date textbox.
Add this script block to your page:-
<script type="text/javascript">
$(document).ready(function() {
$.validator.addMethod("endDate", function(value, element) {
var startDate = $('.startDate').val();
return Date.parse(startDate) <= Date.parse(value) || value == "";
}, "* End date must be after start date");
$('#formId').validate();
});
</script>
Hope this helps :-)
The date values from the text fields can be fetched by jquery's .val() Method like
var datestr1 = $('#datefield1-id').val();
var datestr2 = $('#datefield2-id').val();
I'd strongly recommend to parse the date strings before comparing them. Javascript's Date object has a parse()-Method, but it only supports US date formats (YYYY/MM/DD). It returns the milliseconds since the beginning of the unix epoch, so you can simply compare your values with > or <.
If you want different formats (e.g. ISO 8661), you need to resort to regular expressions or the free date.js library.
If you want to be super user-fiendly, you can use jquery ui datepickers instead of textfields. There is a datepicker variant that allows to enter date ranges:
http://www.filamentgroup.com/lab/date_range_picker_using_jquery_ui_16_and_jquery_ui_css_framework/
In javascript/jquery most of the developer uses From & To date comparison which is string, without converting into date format. The simplest way to compare from date is greater then to date is.
Date.parse(fromDate) > Date.parse(toDate)
Always parse from and to date before comparison to get accurate results
I like what Franz said, because is what I'm using :P
var date_ini = new Date($('#id_date_ini').val()).getTime();
var date_end = new Date($('#id_date_end').val()).getTime();
if (isNaN(date_ini)) {
// error date_ini;
}
if (isNaN(date_end)) {
// error date_end;
}
if (date_ini > date_end) {
// do something;
}
First you split the values of two input box by using split function. then concat the same in reverse order. after concat nation parse it to integer. then compare two values in in if statement. eg.1>20-11-2018 2>21-11-2018
after split and concat new values for comparison 20181120 and 20181121 the after that compare the same.
var date1 = $('#datevalue1').val();
var date2 = $('#datevalue2').val();
var d1 = date1.split("-");
var d2 = date2.split("-");
d1 = d1[2].concat(d1[1], d1[0]);
d2 = d2[2].concat(d2[1], d2[0]);
if (parseInt(d1) > parseInt(d2)) {
$('#fromdatepicker').val('');
} else {
}
this should work
$.validator.addMethod("endDate", function(value, element) {
var startDate = $('#date_open').val();
return Date.parse(startDate) <= Date.parse(value) || value == "";
}, "* End date must be after start date");
you can do on two different dates for alert like this.
$('#end_date').on('change', function(){
var startDate = $('#start_date').val();
var endDate = $('#end_date').val();
if (endDate < startDate){
alert('End date should be greater than Start date.');
$('#end_date').val('');
}
});
either you can perform on calendar it will automatically disable previous dates.
$("#start_date").datepicker({
dateFormat: 'dd/mm/yy',
onSelect: function(date) {
$("#end_date").datepicker('option', 'minDate', date);
}
});
$("#end_date").datepicker({ dateFormat: 'dd/mm/yy' });
If the format is guaranteed to be correct YYYY/MM/DD and exactly the same between the two fields then you can use:
if (startdate > enddate) {
alert('error'
}
As a string comparison