Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am modifying an existing project, where I want to validate the start date(From) and end date(To) using jquery. The requirement is that End date(To) cannot be less than the Start date(from). Below is my html code and I add the jquery code I’ve written. But the date is not validated and it doesn’t display any error message. Once I click the submit button it submit successfully without any error. I really appreciate if some one can help me to resolve this.I tried this so many times, but all are failed. Really appreciate your help Thanks in advance.



===========html code==========
<div class="form-group">
                            <label for="" class="col-sm-2 control-label">Valid From<span class="star"> * </span></label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control datepicker" name="valid_from[]" id="valid_from" required="required">
                            </div>
                            <label for="" class="col-sm-2 control-label"> To <span class="star"> * </span></label>
                            <div class="col-sm-3">
                                <input type="text" class="form-control datepicker" name="valid_to[]" id="valid_to" required="required">
                            </div>
                        </div>



==============jquery validation code==========

var validator = $( "#add_cetificate").validate({
        rules: {
            valid_to[]: {
                greaterThan: "#valid_from"
            },
        }
    });


    $.validator.addMethod('greaterThan', function(value, element) {

            var dateFrom = $("#valid_from").val();
            var dateTo = $('#valid_to').val();

            return dateTo > dateFrom;

});


And also there is a jquery part for date as follows
<pre>$('.datepicker').datepicker({
            format: 'yyyy-mm-dd',
            autoclose: true
        });


What I have tried:

I really appreciate if some one can help me to resolve this.I tried this so many times, but all are failed. Really appreciate your help Thanks in advance.
Posted
Comments
Richard Deeming 3-Feb-20 9:25am    
Your rules definition looks invalid - it should look more like:
rules: {
    "valid_to[]": {
        greaterThan: "#valid_from"
    }
}

valid_to[] is not a valid Javascript identifier, and needs to be enclosed in quotes. You also have a dangling comma, which will cause errors in some browsers.

Beyond that, you need to check your browser's developer tools to see if there are any errors.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900