发送电子邮件ajax之前的电子邮件格式验证

时间:2016-07-02 08:50:21

标签: jquery ajax

我正在尝试调整我在互联网上找到的代码,只在邮件发送之前对我的联系表单的电子邮件字段执行完整性检查(我对其他字段使用type="text" required/

我想出了下面的代码,但这不起作用(单击“提交按钮”表单时没有任何反应)

问题是什么?

非常感谢,

$(document).ready(function() {
    $("#contactfrm").submit(function(e) {
        e.preventDefault();
        var email = $("#email").val();

        function isValidEmail(emailAddress) {
            var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
            return pattern.test(emailAddress);
        };
        if (isValidEmail(email)) {
            $.ajax({
                type: "POST",
                url: "sendmessagetest.php",
                data: dataString,
                success: function() {
                    $('button[name="submit"]').hide();
                    $('.error').hide()
                    $('.success').fadeIn(1000);
                }
            });
        } else {
            $('.error').fadeIn(1000);
        }
        return false;
    });

});

HTML:

 <form class="clearfix" id="contactfrm" method="post" name="contactfrm">
    <div class="tagbox wow fadeInRightBig"><i class="fa fa-pencil"></i> Formulaire de contact
    </div>
    <input class="_input-1 input-40pc" id="first_name" name="first_name" placeholder="Votre Prénom" type="text" required/>
    <input class="_input-1 input-40pc" id="last_name" name="last_name" placeholder="Votre nom de famille" type="text" required/>
    <input class="_input-2 input-40pc" id="email" name="email" placeholder="Votre adresse e-mail" type="text" required/>
    <input class="_input-1 input-40pc" id="phone" name="phone" placeholder="Numéro de téléphone" type="text">
    <textarea id="message" name="message" placeholder="Votre message"></textarea>
    <button class="wow fadeInUp" name="submit" type="submit" value="Envoyer"><i class="fa fa-sign-in"></i> ENVOYER&nbsp;</button>
  </form>

2 个答案:

答案 0 :(得分:1)

试试这个:

function isValidEmail(emailAddress) {
    var pattern = new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);
    return pattern.test(emailAddress);
};

答案 1 :(得分:0)

您的代码产生JS错误:

  

未捕获的ReferenceError:未定义dataString

因为您缺少dataString变量定义(应该是您发送给服务器的数据)。

在旁注上 - 将isValidEmail(emailAddress)方法移到表单submit事件处理程序之外。在每次提交表单时重新定义此方法毫无意义。

$(document).ready(function() {

    // Move the method outside of the form submit event handler:
    function isValidEmail(emailAddress) {
        var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
        return pattern.test(emailAddress);
    };

    $("#contactfrm").submit(function(e) {
        e.preventDefault();

        // define dataString variable (serialize the form):
        var dataString = $(this).serialize();

        var email = $("#email").val();       
        if (isValidEmail(email)) {
            $.ajax({
                type: "POST",
                url: "sendmessagetest.php",
                data: dataString,
                success: function() {
                    $('button[name="submit"]').hide();
                    $('.error').hide();
                    $('.success').fadeIn(1000);
                }
            });
        } else {
            $('.error').fadeIn(1000);
        }

        // no need for this because you have used e.preventDefault();
        // return false;
    });

});

至于问题下方的评论 - 您可以尝试以下方法:

$.ajax({
    type: "POST",
    url: "sendmessagetest.php",
    data: dataString,
    // specify the data type:
    dataType: "json",
    success: function(response) {
        // check server response:
        if(response.status){
            $('button[name="submit"]').hide();
            $('.error').hide();
            $('.success').fadeIn(1000);
        }else{
            $('.success').hide();
            $('.error').text(response.message).fadeIn(1000);
        }
    }
});