標籤: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"> </td>42 <td><input name="Submit" type="button" class="btn_grey" value="儲存" onClick="check(form1)">43 44 <input name="Submit2" type="reset" class="btn_grey" value="重設"> </td>45 </tr>46 </table>47 </form> 48 </body>49 </html>
注意:Regex只能使用"/"開頭和結束,不能使用雙引號,所以你在寫正則的時候要注意哦。