使用JQuery實現郵箱驗證


<head>
    <title></title>
    <style type="text/css">
    .txtbackcolor{background-color:#eee;}
    </style>
    <script src="jquery-1.9.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            var state = false;
            $("#txtEmail").focus(function () {
                if (state == false) {
                    $(this).val('');
                }
            })
            $("#txtEmail").blur(function () {
                if ($(this).val() == '') {
                    $("#spinfo").text("郵箱不能為空");
                    $(this).focus();
                }
                else {
                    if (/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($(this).val()) == false) {
                        $("#spinfo").text("郵箱格式不正確,請重新填寫");
                        $(this).focus();
                    }
                    else {
                        $("#spinfo").text('');
                        $("#spinfo").append("<img src=images/onSuccess.gif />");
                        state=true;
                    }
                }
            })
        })
    </script>
</head>
<body>
 郵箱<input id="txtEmail" type="text" value="輸入郵箱" class="txtbackcolor" /><span id="spinfo"></span><br /></body>

下面就是程序運行時的頁面;

當我鼠標點擊郵箱的文本框時,文本框里面的字消失;

當我什么也不填寫時,就會出現提示信息;

下面是我隨便寫的字母,不符合郵箱格式,彈出錯誤提示;

下面是最后的頁面,當我輸入的郵箱的格式正確時,后面的提示信息變成一張圖片。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM