Jquery表单验证仅适用一次

时间:2015-07-08 11:48:55

标签: jquery forms validation

我正在使用Jquery Form Validation:

        $('#form').validate({
        errorElement: 'span', //default input error message container
        errorClass: 'help-block help-block-error', // default input error message class
        focusInvalid: false, // do not focus the last invalid input
        ignore: "",  // validate all fields including form hidden input
        rules: {
            "cash": {
                equalTo: "#password"
            },

            "custFirstname": {
                required: true
            },
            "custLastname": {
                required: true
            },
            "custEmailaddress": {
                required: true,
                email: true,
            },
            "custmobileno": {
                required: true,
                minlength:9,
                maxlength:10,
                number: true
            },
            "adultFirstname[]": {
                required: () => {
                    return $("input[name='adultFirstname[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                }
            },
            "adultLastname[]":{
                    required: () =>{
                    return $("input[name='adultLastname[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                }
            },
            "adultDob[]": {
                required: () => {
                    return $("input[name='adultDob[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
                dateFormat:true

            },
            "adultPassportno[]": {
                required: () => {
                    return $("input[name='adultPassportno[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
            },

         "childFirstname[]": {
                required: () => {
                    return $("input[name='childFirstname[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
            },
            "childLastname[]": {
                required: () => {
                    return $("input[name='childLastname[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
            },
            "childDob[]": {
                required:() =>{
                    return $("input[name='childDob[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
                dateFormat:true
            },
            "childPassportno[]": {
                required: () => {
                    return $("input[name='childPassportno[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
            },

             "infantFirstname[]": {
                required: () => {
                    return $("input[name='infantFirstname[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
            },
            "infantLastname[]": {
                required: () => {
                    return $("input[name='infantLastname[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
            },
            "infantDob[]": {
                required: () => {
                    return $("input[name='infantDob[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
                dateFormat:true
            },
            "infantPassportno[]": {
                required: () => {
                    return $("input[name='infantPassportno[]']").filter(function() {
                        return $.trim($(this).val()).length > 0
                    }).length == 0
                },
            },
        },
        invalidHandler: (event, validator) => {
            $(".errorfield").hide();
            _.each(validator.errorList, (e: any) => {
                if (e.method == "equalTo") {
                    alert("Payment Gateway is not available please select Points only")

                }
                else if (e.method == "greaterThan") {
                    var confirm = $(".bs-example-modal-sm");
                    confirm.modal('show');
                    $('.modal-body').html(e.message);
                }
                else
                    $(e.element).parent().find("p").text(e.message).show();

            });             

        },

        errorPlacement: function(error, element) { // render error placement for each input type
            var icon = $(element).parent('.input-icon').children('i');
            icon.removeClass('fa-check').addClass("fa-warning");
            //icon.attr("data-original-title", error.text()).tooltip({ 'container': 'body' });
        },

        highlight: function(element) { // hightlight error inputs
            $(element)
                .closest('.form-group').removeClass("has-success").addClass('has-error'); // set error class to the control group   
        },

        unhighlight: function(element) { // revert the change done by hightlight

        },

        success: (label, element) => {
            //$(element).parent().find("p").hide();
            var icon = $(element).parent('.input-icon').children('i');
            $(element).closest('.form-group').removeClass('has-error').addClass('has-success'); // set success class to the control group
            icon.removeClass("fa-warning").addClass("fa-check");
        },

    });     

首次单击表单的提交按钮时,它会很好地验证表单,但如果我们再次点击提交按钮,则表示提交表单而不进行任何验证。

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

在填写表格之前,即表格加载,重置表格。因为它从缓存中获取值。

相关问题