Bootstrap 验证者提交表单

Bootstrap Validator submit form

我现在正在使用 bootstrapValidator.min.js 0.5.3 来验证表单: https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/js/bootstrapValidator.min.js

现在这适用于我将发送邮件事件绑定到的联系表单。

但现在我想将它用于他需要验证表单、发送邮件并提交表单的表单,这样我就可以 $_POST 获取字段以将其写入 mysql。

另请注意,我有一个 foreach 循环,可以在添加更多对象时生成更多表单。 (表格用于申请职位,职位可在后台添加。)

代码:

我不会 post 表格 html 和 PHP 因为我知道它们有效。除非被问到。它是一个非常长的表格,我不想用偏离主题的代码来混淆它。

验证者:

 jQuery('.form-job').each(function (i, obj) {
        jQuery(this).bootstrapValidator({
            fields: {
                firstname: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw naam in.'
                        }
                    }
                },
                lastname: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw achternaam in.'
                        }
                    }
                },
                street: {
                    validators: {
                        stringLength: {
                            min: 5,
                            message: 'Gelieve 5 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw straat in.'
                        }
                    }
                },
                housenumber: {
                    validators: {
                        stringLength: {
                            max: 8,
                            message: 'Vul aub een geldige huisnummer in.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw huisnummer in.'
                        },
                    }
                },
                postal: {
                    validators: {
                        stringLength: {
                            min: 4,
                            max: 8,
                            message: 'Vul aub een geldige postcode in.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw postcode in.'
                        },
                    }
                },
                city: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw stad in.'
                        }
                    }
                },
                country: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw land in.'
                        }
                    }
                },
                email: {
                    validators: {
                        notEmpty: {
                            message: 'Vul aub uw e-mailadres in.'
                        },
                        emailAddress: {
                            message: 'Vul aub een geldig e-mailadres in.'
                        }
                    }
                },
                telephone: {
                    validators: {
                        numeric: {
                            message: 'Gelieve alleen nummers te gebruiken.',
                        },
                        stringLength: {
                            min: 9,
                            message: 'Gelieve 9 of meer cijfers in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub een geldig telefoonnummer in.'
                        }
                    }
                },
                motivation: {
                    validators: {
                        stringLength: {
                            min: 10,
                            max: 550,
                            message: 'Vul aub minstens meer dan 10 letters in en minder dan 550.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw vraag in.'
                        }
                    }
                },
                cvurl: {
                    validators: {
                        file: {
                            extension: 'doc,docx,pdf,txt',
                            type: 'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/txt',
                            maxSize: 5 * 1024 * 1024,   // 5 MB
                            message: 'Het geselecteerd bestand is niet geldig. Dit moet een .doc, .docx, .pdf of een .txt zijn en max 5 MB groot.'
                        }
                    }
                }
            }
        })
            .on('success.form.bv', function (e) {
                jQuery("#button").css('color', 'green');
                jQuery("#button").css('border', '2px solid green');
                jQuery(".alert-success").css('display', 'block');
                jQuery("#button").html('Verstuurd');
                jQuery(this).data('bootstrapValidator').resetForm();

                // Prevent form submission
                e.preventDefault();

                // Get the form instance
                var form = jQuery(e.target);

                // Get the BootstrapValidator instance
                var bv = form.data('bootstrapValidator');

                jQuery.post(my_ajax_object.ajax_url, form.serialize());

            });
    });

请不要介意荷兰语。任何荷兰语都不会影响问题,因为它只是显示的文本。

我也知道它说:

                // Prevent form submission
            e.preventDefault();

但是当我删除它或注释掉它时,没有任何变化。

所以我需要它来验证、发送邮件(有效)并提交。

谢谢!

所以我完成了这项工作。

问题是我试图让 ajax 通过 JS 通过 BootstrapValidator 发送邮件。

所以我最终删除了在 JS 中定义任何内容的部分,我留下了这个:

jQuery('.form-job').each(function (i, obj) {
        jQuery(this).bootstrapValidator({
            fields: {
                firstname: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw naam in.'
                        }
                    }
                },
                lastname: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw achternaam in.'
                        }
                    }
                },
                street: {
                    validators: {
                        stringLength: {
                            min: 5,
                            message: 'Gelieve 5 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw straat in.'
                        }
                    }
                },
                housenumber: {
                    validators: {
                        stringLength: {
                            max: 8,
                            message: 'Vul aub een geldige huisnummer in.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw huisnummer in.'
                        },
                    }
                },
                postal: {
                    validators: {
                        stringLength: {
                            min: 4,
                            max: 8,
                            message: 'Vul aub een geldige postcode in.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw postcode in.'
                        },
                    }
                },
                city: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw stad in.'
                        }
                    }
                },
                country: {
                    validators: {
                        stringLength: {
                            min: 2,
                            message: 'Gelieve 2 of meer letters in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw land in.'
                        }
                    }
                },
                email: {
                    validators: {
                        notEmpty: {
                            message: 'Vul aub uw e-mailadres in.'
                        },
                        emailAddress: {
                            message: 'Vul aub een geldig e-mailadres in.'
                        }
                    }
                },
                telephone: {
                    validators: {
                        numeric: {
                            message: 'Gelieve alleen nummers te gebruiken.',
                        },
                        stringLength: {
                            min: 9,
                            message: 'Gelieve 9 of meer cijfers in te vullen.'
                        },
                        notEmpty: {
                            message: 'Vul aub een geldig telefoonnummer in.'
                        }
                    }
                },
                motivation: {
                    validators: {
                        stringLength: {
                            min: 10,
                            max: 550,
                            message: 'Vul aub minstens meer dan 10 letters in en minder dan 550.'
                        },
                        notEmpty: {
                            message: 'Vul aub uw vraag in.'
                        }
                    }
                },
                cvurl: {
                    validators: {
                        file: {
                            extension: 'doc,docx,pdf,txt',
                            type: 'application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/txt',
                            maxSize: 5 * 1024 * 1024,   // 5 MB
                            message: 'Het geselecteerd bestand is niet geldig. Dit moet een .doc, .docx, .pdf of een .txt zijn en max 5 MB groot.'
                        }
                    }
                }
            }
        })
    });

然后我在提交按钮后调用一个函数

if (isset($_POST['submit'])

(这是一个示例,切勿使用提交作为提交按钮的名称)。

我调用的函数然后发送邮件并对数据库部分使用查询