jquery validate添加自定义开始结束日期验证

添加验证方法:

/****************************添加自定义validate的开始结束时间验证方法*********************/
jQuery.validator.methods.compareDate = function(value, element, param) {
    //var startDate = jQuery(param).val() + ":00";补全yyyy-MM-dd HH:mm:ss格式  
    //value = value + ":00";  
    var startDate = jQuery(param).val();
    var date1 = new Date(Date.parse(startDate.replace("-", "/")));
    var date2 = new Date(Date.parse(value.replace("-", "/")));
    return date1 < date2;
};

使用:

$('#honor_form').validate({
    rules: {
        honor_id: {
            required: true,
        },
        honor_name: {
            required: true,
        },
        honor_banfajigou: {
            required: true,
        },
        get_honor_addr: {
            required: true,
        },
        honor_time_start: {
            required: true,
        },
        honor_time_end: {
            required: true,
            compareDate:"#honor_time_start",
        },
        honor_descript: {
            required: true,
        },
    },
    messages: {
        honor_id: {
            required: "必填项",
        },
        honor_name: {
            required: "必填项",
        },
        honor_banfajigou: {
            required: "必填项",
        },
        get_honor_addr: {
            required: "必填项",
        },
        honor_time_start: {
            required: "必填项",
        },
        honor_time_end: {
            required: "必填项",
            compareDate:"开始时间必须小于结束时间",
        },
        honor_descript: {
            required: "必填项",
        },
    },
    submitHandler: function(form) {
        var fd = new FormData(form);
        var honor_sid = fd.get('honor_sid');
        var honor_id = fd.get('honor_id');
        var honor_name = fd.get('honor_name');
        var honor_banfajigou = fd.get('honor_banfajigou');
        var get_honor_addr = fd.get('get_honor_addr');
        var honor_time_start = fd.get('honor_time_start');
        var honor_time_end = fd.get('honor_time_end');
        var honor_descript = fd.get('honor_descript');
        if (honor_sid == '') {
            $.ajax({
                url: "./index.php?/User/add_honor",
                type: "POST",
                data: {
                    honor_id: honor_id,
                    honor_name: honor_name,
                    honor_banfajigou: honor_banfajigou,
                    get_honor_addr: get_honor_addr,
                    honor_time_start: honor_time_start,
                    honor_time_end: honor_time_end,
                    honor_descript: honor_descript
                },
                // processData:false,
                // contentType: false,
                dataType: "json",
                success: function(e) {
                    if (e) {
                        honor_init();
                        $('#honor_modal').modal('hide');
                        $('#honor_id').attr("value", '');
                        $('#honor_name').attr("value", '');
                        $('#honor_banfajigou').attr("value", '');
                        $('#get_honor_addr').attr("value", '');
                        $('#honor_time_start').attr("value", '');
                        $('#honor_time_end').attr("value", '');
                        $('#honor_descript').attr("value", '');
                    } else {
                        alert("添加失败");
                    }
                }
            });
        } else {
            $.ajax({
                url: "./index.php?/User/edit_honor",
                type: "POST",
                data: {
                    honor_sid: honor_sid,
                    honor_id: honor_id,
                    honor_name: honor_name,
                    honor_banfajigou: honor_banfajigou,
                    get_honor_addr: get_honor_addr,
                    honor_time_start: honor_time_start,
                    honor_time_end: honor_time_end,
                    honor_descript: honor_descript
                },
                // processData:false,
                // contentType: false,
                // dataType:"json",
                success: function(e) {
                    if (e) {
                        $("#honor" + honor_sid).find('#ho_id' + honor_sid).text(honor_id);
                        $("#honor" + honor_sid).find('#ho_na' + honor_sid).text(honor_name);
                        $("#honor" + honor_sid).find('#ho_ba' + honor_sid).text(honor_banfajigou);
                        $("#honor" + honor_sid).find('#ho_ad' + honor_sid).text(get_honor_addr);
                        $("#honor" + honor_sid).find('#ho_t_st' + honor_sid).text(honor_time_start);
                        $("#honor" + honor_sid).find('#ho_t_en' + honor_sid).text(honor_time_end);
                        $("#honor" + honor_sid).find('#ho_de' + honor_sid).text(honor_descript);
                        alert("编辑成功");
                        $('#honor_modal').modal('hide');
                        $('#honor_sid').attr("value", '');
                        $('#honor_id').attr("value", '');
                        $('#honor_name').attr("value", '');
                        $('#honor_banfajigou').attr("value", '');
                        $('#get_honor_addr').attr("value", '');
                        $('#honor_time_start').attr("value", '');
                        $('#honor_time_end').attr("value", '');
                        $('#honor_descript').attr("value", '');
                    } else {
                        alert("编辑失败");
                    }
                }
            });
        }
        return false;
    }

猜你喜欢

转载自blog.csdn.net/zyddj123/article/details/78054855