繁体   English   中英

如何使用jquery验证验证Formspree?

[英]How validate the Formspree with jquery Validation?

我有一个Formspree表单,但没有得到jQuery验证验证

这是我的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中,但不禁止用户发送。

您的代码不会阻止提交!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM