簡體   English   中英

JavaScript電子郵件驗證

[英]Javascript e-mail validation

我通常不會在這樣的地方問問題,因為我可能會發現這些信息可以解決我的問題。 但是,我是PHP,Javascript等的新手,但是似乎無法解決我遇到的這個問題,因此我轉向你們,非常感謝您的幫助。

因此,我正在與一個同伴創建的注釋系統一起玩,但他希望作為一個Javascript函數來檢查電子郵件的有效性。 這是下面的代碼...

$(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);
                    })
                }
            });
        }
    });

});

如您所見,我提供了一個var過濾器等...,但簡要地解釋了一下,最終結果是它會警告說該電子郵件無效,但是無論輸入什么內容,只要輸入有效的電子郵件並嘗試提交,與警報和最終結果保持一致,這本來就不會發生。

抱歉,如果我對此不滿意,或者不了解,但是無論如何我都願意提供幫助。

同樣,任何幫助將不勝感激。

謝謝

有用的功能。

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!");
}

如果要驗證多種輸入類型,則可能要考慮使用諸如parsley.js之類的驗證庫。 我更喜歡香菜,但是有幾個可用的庫。 (注意:歐芹依賴於jquery)。

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

默認情況下,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