<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>正則匹配郵箱</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="description" content="布爾教育 http://www.itbool.com" /> </head> <body> <form action=""> <p>用戶名:<input type="text" name="user"/></p> <p>Email:<input type="text" name="email"/></p> <p><input type="submit" /></p> </form> </body> <script> document.getElementsByTagName('form')[0].onsubmit=function (){ // 阻止提交,part.test()方法用來進行正則匹配 var part=/^[a-zA-Z0-9]{6,11}$/; if(!part.test(document.getElementsByName('user')[0].value)){ alert("請輸入正確的用戶名"); return false; } var partt=/^\w+@\w+(\.\w+)+$/; if(!partt.test(document.getElementsByName('email')[0].value)){ alert("請輸入正確的emial"); return false; } } </script> </html>
選中郵箱信息的變紅:php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>新建網頁</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <input type="submit" value="提交" onclick="find()" /> <ul> <li>張飛<123@163.com></li> <li>劉備15732113942</li> <li>趙雲<921132215@qq.com></li> </ul> </body> <script> function find(){ var lis=document.getElementsByTagName('li'); var patt=/\w+@\w+(\.\w+)+/; var i=0; for (var i = 0; i < lis.length; i++) { if(patt.exec(lis[i].innerHTML)!=null){ lis[i].style.background="red"; } } } </script> </html>