表单有效性校验 jquery的validate

易飞文
2023-12-01

jquery的原生方法实现表单提交的校验和提示

$(function () {
            $('#addPaperForm').validator({
                validate: function (validity) {
                    validity.valid = false;
                    if ($(validity.field).is('#paperName')) {
                        if ($('#paperName').val() == null || $('#paperName').val() == '') {
                            return validity;
                        }
                        return $.ajax({
                            url: '${ctx}/admin/paper/checkPaper',
                            data: {"paper.name": $("#paperName").val()},
                            dataType: 'json',
                        }).then(function (data) {
                            if (data.success) {
                                validity.valid = true;
                            } else {
                                var html = "<tt style='color: red' >试卷名称已存在!</tt>";
                                $("#paperName").next().html("");
                                $("#paperName").next().append(html);
                            }
                            return validity;
                        }, function () {
                            return validity;
                        });
                    } else if ($(validity.field).is("#replyTime")) {
                        if ($('#replyTime').val() != null && $('#replyTime').val() != '') {
                            if ($('#replyTime').val() > 0) {
                                validity.valid = true;
                            }
                        }
                        return validity;
                    } else if ($(validity.field).is("#score")) {
                        if ($('#score').val() != null && $('#score').val() != '') {
                            if ($('#score').val() > 0) {
                                validity.valid = true;
                            }
                        }
                        return validity;
                    } else {
                        if ($(validity.field).val() != null && $(validity.field).val() != '') {
                            validity.valid = true;
                            return validity;
                        } else if ($(validity.field).is("#reexam")) {
                            if ($('#reexam').val() != null && $('#reexam').val() != '') {
                                if ($('#replyTime').val() > 0) {
                                    validity.valid = true;
                                }
                            }
                            return validity;
                        } 
                    }
                },
                submit: function () {
                    var formValidity = this.isFormValid();
                    $.when(formValidity).then(function () {
                        $("#addPaperForm").submit();
                    }, function () {

                    });
                    return false;
                }
            });
        });

 简直不能再好用了

 类似资料: