jQuery-文字框事件應用-判斷郵箱地址

來源:互聯網
上載者:User

標籤:text   ini   padding   image   gif   http   pre   inpu   jscript   

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4     <title>文字框中的事件應用</title> 5     <script type="text/javascript"  6             src="Jscript/jquery-1.8.2.min.js"> 7     </script> 8     <style type="text/css"> 9             body{font-size:13px} 10             /* 元素初始狀態樣式 */11             .divInit{width:390px;height:55px;line-height:55px;padding-left:20px}12             .txtInit{border:#666 1px solid;padding:3px;background-image:url(‘Images/bg_email_input.gif‘)}13             .spnInit{width:179px;height:40px;line-height:40px;float:right;margin-top:8px;padding-left:10px;background-repeat:no-repeat}14             15             /* 元素丟失焦點樣式 */16             .divBlur{background-color:#FEEEC2}17             .txtBlur{border:#666 1px solid;padding:3px;background-image:url(‘Images/bg_email_input2.gif‘)}18             .spnBlur{background-image:url(‘Images/bg_email_wrong.gif‘)}19 20             .divFocu{background-color:#EDFFD5}/* div擷取焦點樣式 */21             .spnSucc{background-image:url(‘Images/pic_Email_ok.gif‘);margin-top:20px}/* 驗證成功時span樣式 */22     </style>23     <script type="text/javascript">24         $(function() {25             $("#txtEmail").trigger("focus");//預設時文字框擷取焦點26 27             $("#txtEmail").focus(function() {//文字框擷取焦時間點事件28                 $(this).removeClass("txtBlur").addClass("txtInit");29                 $("#email").removeClass("divBlur").addClass("divFocu");30                 $("#spnTip").removeClass("spnBlur")31                 .removeClass("spnSucc").html("請輸入您常用郵箱地址!");32             })33 34             $("#txtEmail").blur(function() {//文字框丟失焦時間點事件35                 var vtxt = $("#txtEmail").val();36                 if (vtxt.length == 0) {37                     $(this).removeClass("txtInit").addClass("txtBlur");38                     $("#email").removeClass("divFocu").addClass("divBlur");39                     $("#spnTip").addClass("spnBlur").html("郵箱地址不可為空!");40                 }41                 else {42                     if (!chkEmail(vtxt)) {//檢測郵箱格式是否正確43                         $(this).removeClass("txtInit").addClass("txtBlur");44                         $("#email").removeClass("divFocu").addClass("divBlur");45                         $("#spnTip").addClass("spnBlur").html("郵箱格式不正確!");46                     } 47                     else {//如果正確48                         $(this).removeClass("txtBlur").addClass("txtInit");49                         $("#email").removeClass("divFocu");50                         $("#spnTip").removeClass("spnBlur").addClass("spnSucc").html("");51                     }52                 }53             })54             /*55              *驗證郵箱格式是否正確56              *參數strEmail,需要驗證的郵箱57             */58             function chkEmail(strEmail) {59                 if (!/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(strEmail)) {60                     return false;61                 }62                 else {63                     return true;64                 }65             }66         })67     </script>68 </head>69 <body>70    <form id="form1" action="#">71       <div id="email" class="divInit">郵箱:72            <span id="spnTip" class="spnInit"></span>73            <input id="txtEmail" type="text" class="txtInit" />74       </div>75    </form>76 </body>77 </html>

 

jQuery-文字框事件應用-判斷郵箱地址

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.