Quantcast
Channel: Detecting an "invalid date" Date instance in JavaScript - Stack Overflow
Viewing latest article 25
Browse Latest Browse All 58

Answer by kam for Detecting an "invalid date" Date instance in JavaScript

$
0
0

Date object to string is more simple and reliable way to detect if both fields are valid date. e.g. If you enter this "-------" to the date input field. Some of the above answers won't work.

jQuery.validator.addMethod("greaterThan", 

    function(value, element, params) {
        var startDate = new Date($(params).val());
        var endDate = new Date(value);

        if(startDate.toString() === 'Invalid Date' || endDate.toString() === 'Invalid Date') {
            return false;
        } else {
            return endDate > startDate;
        }
    },'Must be greater than {0}.');

Viewing latest article 25
Browse Latest Browse All 58

Trending Articles