- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- 明日科技
- 462字
- 2020-11-28 17:36:36
6.4 應用正則表達式對用戶注冊信息進行驗證
【例6.6】通過正則表達式對用戶注冊信息的合理性進行判斷,對用戶輸入的郵編、電話號碼、郵箱地址和網址的格式進行判斷。本例中應用正則表達式和JavaScript腳本,判斷用戶輸入信息的格式是否正確。實例代碼如下:(實例位置:光盤\TM\sl\6\6)
首先,在index.php頁面中通過Script腳本調用js腳本文件check.js,創建form表單,實現會員注冊信息的提交,并應用onSubmit事件調用chkreg()方法對表單元素中的數據進行驗證,將數據提交到index_ok.php文件中。index.php的關鍵代碼如下:
<script src="js/check.js"></script> <form name="reg_check"method="post"action="index_ok.php"onSubmit="return chkreg(reg check, 'all')"> <table width="550" height="270" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="30"><div align="right">郵政編碼:</div></td> <td height="30" colspan="2" align="left"> <input type="text"name="postalcode"size="20"onBlur="chkreg(reg check,2)"> <div id="check postalcode"style="color:#F1B000"></div> </td> </tr> <tr> <td height="30"><div align="right">E-mail:</div></td> <td height="30" colspan="2" align="left"> <input type="text" name="email" size="20" onBlur="chkreg(reg_check,4)"> <font color="#999999">請務必正確填寫您的郵箱</font> <div id="check email"style="color:#F1B000"></div> </td> </tr> <tr> <td height="30" align="right">固定電話:</td> <td height="30" colspan="2" align="left"> <input type="text"name="gtel"size="20" onBlur="chkreg(reg check,6)"> <font color="#999999"><div id="check gtel"style="color:#F1B000"></div></font></td> </tr> <tr> <td height="30"><div align="right">移動電話:</div></td> <td height="30" colspan="2" align="left"> <input type="text"name="mtel"size="20" onBlur="chkreg(reg check,5)"> <div id="check_mtel" style="color:#F1B000"></div></td> </tr> <tr> <td width="100"height="30"><input type="image" src="images/bg_09.jpg"></td> <td width="340"><img src="images/bg_11.jpg" width="56" height="30" onClick="reg_check.reset()" style="cursor:hand"/></td> </tr> </table> </form>
在check.js腳本文件中,創建自定義方法,應用正則表達式對會員注冊的電話號碼和郵箱進行驗證。其關鍵代碼如下:
function checkregtel(regtel){ var str=regtel; var Expression=/^13(\d{9})$|^18(\d{9})$|^15(\d{9})$/; //驗證手機號碼 var objExp=new RegExp(Expression); if(objExp.test(str)==true){ return true; }else{ return false; } } function checkregtels(regtels){ var str=regtels; var Expression=/^(\d{3}-)(\d{8})$|^(\d{4}-)(\d{7})$|^(\d{4}-)(\d{8})$/; //驗證座機號碼 var objExp=new RegExp(Expression); if(objExp.test(str)==true){ return true; }else{ return false; } } function checkregemail(emails){ var str=emails; var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; //驗證郵箱地址 var objExp=new RegExp(Expression); if(objExp.test(str)==true){ return true; }else{ return false; } }
運行結果如圖6.3所示。

圖6.3 應用正則表達式對用戶注冊信息進行驗證
注意
在本例中通過正則表達式對表單提交的數據進行驗證,在JavaScript腳本中,應用onBlur事件調用對應的方法對表單提交的數據直接進行驗證,并通過div標簽返回結果。
推薦閱讀
- C和C++安全編碼(原書第2版)
- 實用防銹油配方與制備200例
- Nginx Essentials
- Drupal 8 Module Development
- 深入淺出PostgreSQL
- Unreal Engine 4 Shaders and Effects Cookbook
- 單片機C語言程序設計實訓100例
- PHP+MySQL+Dreamweaver動態網站開發從入門到精通(第3版)
- AIRIOT物聯網平臺開發框架應用與實戰
- OpenCV 3計算機視覺:Python語言實現(原書第2版)
- 硬件產品設計與開發:從原型到交付
- 寫給大家看的Midjourney設計書
- Android應用開發實戰(第2版)
- C/C++代碼調試的藝術(第2版)
- 嵌入式C編程實戰