简体   繁体   English

JavaScript电子邮件验证

[英]Javascript e-mail validation

I usually don't ask questions in places like this as its filled with information that I probably could find that will solve my problem. 我通常不会在这样的地方问问题,因为我可能会发现这些信息可以解决我的问题。 However, I am new to PHP, Javascript etc.. and I just can't seem to grasp this problem I am having, so I turn to you guys and would greatly appreciate the help. 但是,我是PHP,Javascript等的新手,但是似乎无法解决我遇到的这个问题,因此我转向你们,非常感谢您的帮助。

So, I am playing about with a comment system a fellow created but wanted to as a Javascript function that would check the validation of an email. 因此,我正在与一个同伴创建的注释系统一起玩,但他希望作为一个Javascript函数来检查电子邮件的有效性。 Here is the code as followed... 这是下面的代码...

$(function () {
    //alert(event.timeStamp);
    $('.new-com-bt').click(function (event) {
        $(this).hide();
        $('.new-com-cnt').show();
        $('#name-com').focus();
    });

    /* when start writing the comment activate the "add" button */
    $('.the-new-com').bind('input propertychange', function () {
        $(".bt-add-com").css({
            opacity: 0.6
        });
        var checklength = $(this).val().length;
        if (checklength) {
            $(".bt-add-com").css({
                opacity: 1
            });
        }
    });

    /* on clic  on the cancel button */
    $('.bt-cancel-com').click(function () {
        $('.the-new-com').val('');
        $('.new-com-cnt').fadeOut('fast', function () {
            $('.new-com-bt').fadeIn('fast');
        });
    });

    // on post comment click 
    $('.bt-add-com').click(function () {
        var theCom = $('.the-new-com');
        var theName = $('#name-com');
        var theMail = $('#mail-com');
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]    {2,4})+$/;

        if (!theCom.val()) {
            alert('oh! Don`t forget your message!');
        }
        if (!filter.test(theMail.value)) {
            alert('Please provide a vaild email address');
            theMail.focus;
            return false;

        } else {
            $.ajax({
                type: "POST",
                url: "ajax/add-comment.php",
                data: 'act=add-com&id_post=' + <? php echo $id_post; ? > +'&name=' + theName.val() + '&email=' + theMail.val() + '&comment=' + theCom.val(),
                success : function (html) {
                    theCom.val('');
                    theMail.val('');
                    theName.val('');
                    $('.new-com-cnt').hide('fast', function () {
                        $('.new-com-bt').show('fast');
                        $('.new-com-bt').before(html);
                    })
                }
            });
        }
    });

});

As you can see I provided a var filter etc... but explaining in brief terms, the end result is it will alert to say the email is invalid but when typing in a valid email and trying to submit, no matter what it just comes up with the alert and the end result, which is meant to happen simply doesn't. 如您所见,我提供了一个var过滤器等...,但简要地解释了一下,最终结果是它会警告说该电子邮件无效,但是无论输入什么内容,只要输入有效的电子邮件并尝试提交,与警报和最终结果保持一致,这本来就不会发生。

Sorry if I going to long way round about this or not having any knowledge of this, but i am willing to assist in anyway. 抱歉,如果我对此不满意,或者不了解,但是无论如何我都愿意提供帮助。

Again any help would be much appreciated. 同样,任何帮助将不胜感激。

Thanks 谢谢

A useful function. 有用的功能。

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.    [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 


// Example
if(validateEmail("test@test.com"))
{
    alert("Valid!");
}else{
    alert("Invalid!");
}

You might want to consider using a validation library like parsley.js if you will be validating multiple input types. 如果要验证多种输入类型,则可能要考虑使用诸如parsley.js之类的验证库。 I prefer parsley, but there are several libraries available. 我更喜欢香菜,但是有几个可用的库。 (note: parsley relies on jquery). (注意:欧芹依赖于jquery)。

<form id="emailForm" data-parsley-validate>
   <label for="email">Email * :</label>
   <input type="email" name="email" required />
</form>

By default, parsley will show a generic error message like "This field is required. This should be a valid email.", but you can easily customize the message, or opt-out of displaying any message at all. 默认情况下,parsley将显示一般错误消息,例如“此字段是必填字段。这应该是有效的电子邮件。”,但是您可以轻松地自定义该消息,或者选择不显示任何消息。

data-parsley-error-message="my message" // A global error message for this input
data-parsley-email-message="my message" // Replace the default parsley email error message for this input

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

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