首页 > Web开发 > 详细

Jquery validate自定义验证

时间:2017-07-03 09:41:47      阅读:397      评论:0      收藏:0      [点我收藏+]

 

    <script type="text/javascript">
        var validator;
        $(function() {

            $.validator.addMethod("compareDate", function(value, element) {
                var dateStart = $("#dateStart").val();
                var dateEnd = $("#dateEnd").val();
                return dateStart <= dateEnd;
            }, "开始日期必须早于结束日期");

            $.validator.addMethod("checkMedicalExist",
                    function(value, element) {
                        var result = true;
                        var address = $.trim($("#address").val());
                        var dateStart = $("#dateStart").val();
                        var dateEnd = $("#dateEnd").val();
                        if (address == ‘‘ || dateStart == ‘‘ || dateEnd == ‘‘) {
                            return true;
                        }
                        $.ajax({
                            type : "POST",
                            async : false,
                            url : "${ctx}/meeting/medical/checkMedical",
                            data : {
                                "address" : address,
                                "dateStart" : dateStart,
                                "dateEnd" : dateEnd
                            },
                            dataType : ‘json‘,
                            success : function(d) {
                                if (d.boolStatus == true) {
                                    var date = d.data;
                                    jBox.tip(date+‘已经存在医疗地点‘);
                                    result = false;
                                } else  {
                                    result = true;
                                }
                            }
                        });
                        return result;
                    }, "日期有冲突" );

            $.validator.addMethod("compareTime", function(value, element) {
                var timeStart = $("#timeStart").val();
                var timeEnd = $("#timeEnd").val();
                return timeStart < timeEnd;
            }, "开始时间必须早于结束时间");

            validator = $("#inputForm")
                    .validate(
                            {
                                rules : {
                                    address : {
                                        required : true
                                    },
                                    dateStart : {
                                        required : true
                                    },
                                    dateEnd : {
                                        required : true,
                                        compareDate : true,
                                        checkMedicalExist : true
                                    },
                                    timeStart : {
                                        required : true,
                                    },
                                    timeEnd : {
                                        required : true,
                                        compareTime : true
                                    },
                                    ambulanceNum : {
                                        required : true
                                    }
                                    /* name:{required:true},
                                    phone:{required:true} */
                                },
                                submitHandler : function(form) {
                                    $
                                            .ajax({
                                                type : "POST",
                                                asyn : false,
                                                url : ‘${ctx}/meeting/medical/saveMedical‘,
                                                data : $(‘#inputForm‘)
                                                        .serialize(),// 要提交的表单
                                                dataType : ‘json‘,
                                                success : function(data) {
                                                        if (data.status == ‘1‘) {
                                                            //parent.jBox.tip(‘保存成功‘);
                                                            window.parent.window.isFreshFlag=‘2‘;//刷新父页面
                                                            parent.$.jBox.close();
                                                        } else if (data.status == ‘0‘) {
                                                            if(data.boolStatus == ‘true‘){
                                                                jBox.tip(‘日期:------重复‘);
                                                            }else{
                                                                jBox.tip(‘保存失败,请重试‘);
                                                            }
                                                        }
                                                }
                                            });
                                }

                            });

        })
    </script>

 

Jquery validate自定义验证

原文:http://www.cnblogs.com/miye/p/7109518.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!