Validate end_date is after start_date [duplicate]

本秂侑毒 提交于 2019-12-12 05:17:07

问题


I have a form with two date fields, start_date and end_date. I want to create a rule that end_date must be greater then start_date, and if this condition returns false then to show validation errors as in the picture below.

So far I've tried to do so by creating a custom rule:

$.validator.addMethod("check_date", function(value, element) {
    var start_date = $("input[name='start_date']").val();
    var end_date = $("input[name='end_date']").val();
    return end_date(value) > start_date(value);
}, 'End date must be greater then start date.');

I'm not sure how exactly I set the rule and the message.


回答1:


Got it.

   $.validator.addMethod("check_date", function(value, element) {
    var start_date = $("input[name='start_date']").val();
    var end_date = $("input[name='end_date']").val();
    return end_date > start_date;
}, 'End date must be greater then Start date.');


来源:https://stackoverflow.com/questions/40101500/validate-end-date-is-after-start-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!