Jquery 郵箱的驗證

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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').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 />電話<input id="txtPhone" type="text" />
</body>
</html>

發佈了133 篇原創文章 · 獲贊 3 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章