【问题标题】:How validate the Formspree with jquery Validation?如何使用 jquery Validation 验证 Formspree?
【发布时间】:2015-06-15 03:04:06
【问题描述】:

我有一个带有 Formspree 的表单,但没有得到 jQuery Validation 的验证。

这是我的 HTML 代码:

<form class="form" id="contactForm" action="//formspree.io/myemail@email.com" method="post">
    <input type="text" name="Name" placeholder="Your name" required>
    <input type="email" name="Email" placeholder="Email" required>
    <input type="tel" name="Phone" placeholder="Phone">
    <input type="text" name="Subject" placeholder="Subject">
    <textarea name="Message" cols="30" rows="10" placeholder="Message" required></textarea>
    <!-- CONFIG -->
    <input class="is-hidden" type="text" name="_gotcha">
    <input type="hidden" name="_subject" value="Contact by Site">
    <input type="hidden" name="_cc" value="cc@email.com">
    <!-- /CONFIG -->
    <input class="submit" type="submit" value="Send">
</form>

这是我的 JS 代码:

(function() {

    // FORM / CONTACT

    var $contactForm = $('#contactForm');
    // VALIDATE FORM
    $contactForm.validate();
    $contactForm.submit(function(e) {
        e.preventDefault();
        $.ajax({
            url: '//formspree.io/myemail@mail.com',
            method: 'POST',
            data: $(this).serialize(),
            dataType: 'json',
            beforeSend: function() {
                $contactForm.append('<div class="message message--loading">Sending message…</div>');
            },
            success: function(data) {
                $contactForm.find('.alert--loading').hide();
                $contactForm.append('<div class="message message--success">Message sent!</div>');
            },
            error: function(err) {
                $contactForm.find('.alert--loading').hide();
                $contactForm.append('<div class="message message--error">Ops, there was an error.</div>');
            }
        });
    });
})();

此时,消息以白色字段发送。在我的情况下,验证仅出现在 CSS 中,但不禁止用户发送。

【问题讨论】:

    标签: javascript jquery html ajax validation


    【解决方案1】:

    您的代码不会阻止提交!

    (function() {
        var $contactForm = $('#contactForm');
        // VALIDATE FORM
        $contactForm.validate();
        $contactForm.submit(function(e) {
            if ($(this).valid())
                 {...}
           return false;
         });
    })();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-04
      • 2017-01-02
      相关资源
      最近更新 更多