Js使用Regex判斷郵箱格式

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   使用   

  

 1 <html> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 4 <title>驗證Email是否正確</title> 5 <script language="javascript"> 6 function checkemail(str){ 7  //在JavaScript中,Regex只能使用"/"開頭和結束,不能使用雙引號 8 var Expression=/\w+([-+.‘]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;  9 var objExp=new RegExp(Expression);10 if(objExp.test(str)==true){11     return true;12 }else{13     return false;14 }15 }16 function check(myform){17     if(myform.email.value==""){18         alert("請輸入Email地址!");myform.email.focus();return;19     }20     if(!checkemail(myform.email.value)){21         alert("您輸入Email地址不正確!");myform.email.focus();return;22     }23     myform.submit();24 }25 </script>26 </head>27 <body>28 <form name="form1" method="post" action="">29 <table width="100%" height="276" border="0" cellpadding="0" cellspacing="0">30           <tr>31             <td width="16%" height="36" align="center">留言人:</td>32             <td width="84%">33               <input name="author" type="text" id="author" size="30" title="留言人"></td>34           </tr>35           <tr>36             <td height="38" align="center">Email:</td>37             <td><input name="email" type="text" id="email" size="72" title="Email地址">38                </td>39           </tr>40           <tr>41             <td height="42" align="center">&nbsp;</td>42             <td><input name="Submit" type="button" class="btn_grey" value="儲存" onClick="check(form1)">43               &nbsp;&nbsp;44               <input name="Submit2" type="reset" class="btn_grey" value="重設">&nbsp;&nbsp;</td>45           </tr>46         </table>47 </form>    48 </body>49 </html>

  注意:Regex只能使用"/"開頭和結束,不能使用雙引號,所以你在寫正則的時候要注意哦。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.